— 02-spring的bean管理注解准备
注解可以写在类 属性 方法上
创建动态网站 spring_day02_demo 选择 2.5
导入 D:\qudong\spring\01-相关jar包\spring Ioc基本包 D:\qudong\spring\02-spring的aop
— bean1.xml
<!– 开启注解扫描
(1)到包里面扫描类、方法、属性上面是否有注解
–>
<context:component-scan base-package=”cn.itcast”></context:component-scan>
<!–
扫描属性上面的注解 开发中一般不用
–>
<context:annotation-config></context:annotation-config>
— 03-spring的bean管理(注解创建对象)
spring 提供的@component 衍生注解 : @Repository 、@Service 和 @Controller http://blog.csdn.net/tjcyjd/article/details/37903429
package cn.itcast.anno;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service(value=”userX”) // <bean id=”userX” class=””/>
@Scope(value=”prototype”)
public class User {
public void add() {
System.out.println(“add………..”);
}
}
—
package cn.itcast.anno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testUser() {
ApplicationContext context =
new ClassPathXmlApplicationContext(“bean1.xml”);
User user = (User) context.getBean(“userX”);
System.out.println(user);
user.add();
}
}
— 04-spring的bean管理(注解注入属性)
有两种注入方法 : 1 : @Autowired 2 : @Resource
Spring注解@Resource和@Autowired区别对比 : https://www.cnblogs.com/think-in-java/p/5474740.html
package cn.itcast.anno;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service(value=”userServiceX”)
public class UserService {
//得到dao对象
//1 定义dao类型属性
//在dao属性上面使用注解 完成对象注入
//
@Autowired
//
private UserDao userDao;
// 使用注解方式时候不需要set方法
// name属性值 写 注解创建dao对象 value值
@Resource(name=”userDaoX”)
private UserDao userDao;
public void add() {
System.out.println(“service………..”);
userDao.add();
}
}
—
package cn.itcast.anno;
import org.springframework.stereotype.Component;
@Component(value=”userDaoX”)
public class UserDao {
public void add() {
System.out.println(“dao………”);
}
}
—
package cn.itcast.anno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testService() {
ApplicationContext context =
new ClassPathXmlApplicationContext(“bean1.xml”);
UserService userService = (UserService) context.getBean(“userServiceX”);
userService.add();
}
}
— 05-spring的bean管理(注解和配置文件混合使用)
创建对象用配置文件
注入属性用注解
package cn.itcast.xmlanno;
public class OrdersDao {
public void buy() {
System.out.println(“ordersdao……….”);
}
}
—
package cn.itcast.xmlanno;
public class BookDao {
public void book() {
System.out.println(“bookdao……….”);
}
}
—
package cn.itcast.xmlanno;
import javax.annotation.Resource;
public class BookService {
//得到bookdao和ordersdao对象
@Resource(name=”bookDao”)
private BookDao bookDao;
@Resource(name=”ordersDao”)
private OrdersDao ordersDao;
public void add() {
System.out.println(“service………”);
bookDao.book();
ordersDao.buy();
}
}
— bean2.xml
<!– 开启注解扫描
(1)到包里面扫描类、方法、属性上面是否有注解
–>
<context:component-scan base-package=”cn.itcast”></context:component-scan>
<!– 配置对象 –>
<bean id=”bookService” class=”cn.itcast.xmlanno.BookService”></bean>
<bean id=”bookDao” class=”cn.itcast.xmlanno.BookDao”></bean>
<bean id=”ordersDao” class=”cn.itcast.xmlanno.OrdersDao”></bean>
—
package cn.itcast.xmlanno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testService() {
ApplicationContext context =
new ClassPathXmlApplicationContext(“bean2.xml”);
BookService bookService = (BookService) context.getBean(“bookService”);
bookService.add();
}
}
— 06-aop原理(一)
—