分四块
1.如何将使用注解当前类放入到容器?
将当前类放入Spring容器(如果不添加名称,则以当前类名为默认名称)
@Component("user")//相当于<bean name="user" class="cn.itcast.bean.User"></bean>
下面三个注解从功能上来说与component一样,都是将当前类放入Spring容器中,只不过使用下面三个注解在实际开发中,比较好区分当前类在哪一层
@Service //service层
@Repository //dao层
@Controller //action层
下面是具体案例:
指定扫描注解路径
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--
指定扫描cn.itcast.bean包下所有类中的注解
注意:扫描包时,会扫描指定报下的所有子孙包
-->
<context:component-scan base-package="cn.itcast.bean"></context:component-scan>
</beans>
写一个测试类
/*@Service //service层
@Repository //dao层
@Controller //action层
相当于<bean name="user" class="cn.itcast.bean.User"></bean>
*/@Component("user")
public class User {
private String name ;
private Integer age ;
private Car car ;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public void init ()
{
System.out.println("init");
}
public void destroy ()
{
System.out.println("destroy");
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}
测试
public class Demo {
@Test
public void testAnnotation ()
{
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml") ;
User user = (User) ac.getBean("user") ;
System.out.println(user);
}
}
2.作用范围
以多例模式来创建(不写即默认单例)
@Scope(scopeName="prototype")
3.属性注入
值类型注入
@Value
两种用法:
1,放到成员变量
//将tom赋给name属性
@Value("TOM")
private String name ;
2.放到set方法上
@Value("TOM")
public void setName(String name) {
this.name = name;
}
区别:
第一种情况是通过反射的field直接赋值,直接对name赋值
第二种情况是通过set方法进行赋值
按道理讲应该推荐使用set赋值,因为第一种破坏了封装性,但是使用set赋值又会使bean中代码变得比较乱,所以你随意喽
对象类型注入
首先你要把要注入的那个对象也加入到容器中
@Component("car")
public class Car {
@Autowired
//局限:如果匹配到多个类型一致的对象,无法选择具体注入哪个对象
@Autowired //自动装配 从容器中找当前要赋值类型的对象(Car),然后付给他
private Car car ;
@Qualifier要和@Autowired一起用才有效
@Autowired
@Qualifier("car")//告诉Spring容器自动装配那个对象
private Car car ;
@Resource 推荐
//如果不加入name,则根据这个字段去IOC容器中找相同类型(在容器中要确保改类型只有一个变量)
@Resource(name="car")//手动注入,指定注入哪个名称的对象
private Car car ;
4.初始化与销毁
@PostConstruct
@PreDestroy
@PostConstruct //初始化,在对象创建后调用
public void init ()
{
System.out.println("init");
}
@PreDestroy //在销毁之前调用
public void destroy ()
{
System.out.println("destroy");
}
@Test
public void testAnnotation ()
{
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml") ;
User user = (User) ac.getBean("user") ;
ac.close();
}
总结:
1) 使用注解,可以简化配置,且可以把对象加入IOC容器,及处理依赖关系(DI)
2) 注解可以和XML配置一起使用。