@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。
有 Person 类有个 Car 类型的属性
Car类
public class Car {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
'}';
}
}
@Autowired 注解用在setter方法上
使用@Autowired注解放在setter方法来摆脱在XML配置文件中的 <property>元素。当Spring发现setter方法使用@ Autowired注解,它会尝试对方法进行byType的自动装配。
public class Person {
private Car car;
public Car getCar() {
return car;
}
@Autowired
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person{" +
"car=" + car +
'}';
}
}
xml配置
<?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:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="people" class="com.spring.demo4.Person">
</bean>
<bean id="car" class="com.spring.demo4.Car">
<property name="name" value="baoma"/>
</bean>
</beans>
@Autowired 注解在属性上
作用在属性上时就可以省略对应的get、set方法,xml配置与上边的xml配置一样即可。
public class Person {
@Autowired
private Car car;
@Override
public String toString() {
return "Person{" +
"car=" + car +
'}';
}
}
@Autowired作用在构造函数上
构造函数和@Autowired注解表明该构造函数应该在创建这个bean时,自动装配,即使在配置bean的XML文件没有<constructor-arg>元素被使用。xml的配置与上边的一致。
public class Person {
private Car car;
@Autowired
public Person(Car car){
this.car = car;
}
@Override
public String toString() {
return "Person{" +
"car=" + car.getName() +
'}';
}
}
我们会发现在使用@Autowired时,xml中有<context:annotation-config></context:annotation-config>配置。
<context:annotation-config />的作用
<context:annotation-config />的作用是向Spring容器注册以下四个BeanPostProcessor:
-
AutowiredAnnotationBeanPostProcessor
-
CommonAnnotationBeanPostProcessor
-
PersistenceAnnotationBeanPostProcessor
-
RequiredAnnotationBeanPostProcessor
为了能让系统更快的识别相应的注解,所以需要配置上边四个BeanPostProcessor。
如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
传统声明方式如下:
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
2、如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor Bean。
3、如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
4、如果想使用@Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。
以上这些注解是很常用的,如果按照传统的方式进行配置将会非常繁琐,所以Spring给我们提供了一个简便的方式:<context:annotation-config/>,使用该元素可以自动声明以上注解。
注:由于<context:component-scan base-package=”xx.xx”/>也包含了自动注入上述Bean的功能,所以<context:annotation-config/> 可以省略。如果两者都进行了配置,则只有前者有效。
一些细节
- 如果使用 @Autowired 作用在应用类型的属性上,Spring容器会自动装配,如果在 xml 配置了多个这个引用类的 Bean,Spring会从上往下扫描xml,会装配第一个查找到的bean。
- 默认情况下, 所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时, 会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
- 默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
- @Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
- @Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.