29 Spring使用@Autowired注解自动装配
在上几篇 Spring自动装配 示例中,它会匹配当前Spring容器任何bean的属性自动装配。在大多数情况下,你可能只需要在特定的 bean 自动装配属性。
在Spring中,可以使用 @Autowired 注解通过setter方法,构造函数或字段自动装配Bean。此外,它可以在一个特定的bean属性自动装配。
- 注 : @Autowired注解是通过匹配数据类型自动装配Bean。
请参见下面的完整的例子来演示如何使用@Autowired
1. Beans
一个 Customer bean 在bean配置文件中声明。稍后,您将使用 “@Autowired” 来自动装配一个Person bean。
package com.gp6.autoAssemblyBean.autowired;
public class Person {
private String name;
private String age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.gp6.autoAssemblyBean.autowired;
import org.springframework.beans.factory.annotation.Autowired;
public class Customer {
@Autowired
private Person person;
private int type;
private String action;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="CustomerBean" class="com.gp6.autoAssemblyBean.autowired.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
<!-- 放开下面这一行注释,才能将person的属性注入到CustomerBean -->
<!-- <property name="person" ref="PersonBean" /> -->
</bean>
<bean id="PersonBean" class="com.gp6.autoAssemblyBean.autowired.Person">
<property name="name" value="gp6" />
<property name="address" value="address 123" />
<property name="age" value="28" />
</bean>
</beans>
package com.gp6.autoAssemblyBean.autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main( String[] args ) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/gp6/autoAssemblyBean/autowired/applicationContext.xml");
Customer customer = (Customer)context.getBean("CustomerBean");
System.out.println(customer.getPerson().getName());
}
}
此时运行会报错,因为person属性未能注入到Customer中
2. 注册AutowiredAnnotationBeanPostProcessor
要启用@Autowired,必须注册“AutowiredAnnotationBeanPostProcessor',你可以用两种方式做到这一点:
- a). Include <context:annotation-config />
添加 Spring 上下文和<context:annotation-config />在bean配置文件中。
<beans
//...
xmlns:context="http://www.springframework.org/schema/context"
//...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
//...
<context:annotation-config />
//...
</beans>
下面是完整实例
<!-- 通过<context:annotation-config />注册AutowiredAnnotationBeanPostProcessor -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="CustomerBean" class="com.gp6.autoAssemblyBean.autowired.registerAutowiredAnnotationBeanPostProcessor.first.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean" class="com.gp6.autoAssemblyBean.autowired.registerAutowiredAnnotationBeanPostProcessor.first.Person">
<property name="name" value="gp6First" />
<property name="address" value="address 123" />
<property name="age" value="28" />
</bean>
</beans>
Customer,Person,Test类和上面完全一致,只是所在包不同,注意Test类中的配置文件地址
- b) 包含 AutowiredAnnotationBeanPostProcessor
直接在bean配置文件包含“AutowiredAnnotationBeanPostProcessor”。
<!-- 直接包含 AutowiredAnnotationBeanPostProcessor -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="CustomerBean" class="com.gp6.autoAssemblyBean.autowired.registerAutowiredAnnotationBeanPostProcessor.second.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean" class="com.gp6.autoAssemblyBean.autowired.registerAutowiredAnnotationBeanPostProcessor.second.Person">
<property name="name" value="gp6Second" />
<property name="address" value="address 123" />
<property name="age" value="28" />
</bean>
</beans>
Customer,Person,Test类类和上面完全一致,只是所在包不同,注意Test类中的配置文件地址
3. @Autowired示例
现在,你可以通过 @Autowired 自动装配 bean,它可以在 setter 方法,构造函数或字段中使用。
package com.gp6.autoAssemblyBean.autowired.example;
import org.springframework.beans.factory.annotation.Autowired;
public class Customer {
//1. @Autowired 字段
@Autowired
private Person person;
private int type;
private String action;
//2. @Autowired 构造方法
//@Autowired
public Customer(Person person) {
this.person = person;
}
public Person getPerson() {
return person;
}
//3 @Autowired setter 方法
//@Autowired
public void setPerson(Person person) {
this.person = person;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
}
上面的3种方法都会自动装配“PersonBean”到Customer的person属性。
Person,Test类类和上面完全一致,只是所在包不同,注意Test类中的配置文件地址
依赖检查
默认情况下,@Autowired将执行相关检查,以确保属性已经装配正常。当Spring无法找到匹配的Bean装配,它会抛出异常。要解决这个问题,可以通过 @Autowired 的“required”属性设置为false来禁用此检查功能。
package com.gp6.autoAssemblyBean.autowired.example;
import org.springframework.beans.factory.annotation.Autowired;
public class Customer {
//1. @Autowired 字段
@Autowired(required=false)
private Person person;
private int type;
private String action;
//2. @Autowired 构造方法
//@Autowired(required=false)
public Customer(Person person) {
this.person = person;
}
public Person getPerson() {
return person;
}
//3 @Autowired setter 方法
//@Autowired(required=false)
public void setPerson(Person person) {
this.person = person;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
}
在上面的例子中,如果Spring不能找到一个匹配的Bean,person属性将不设定。
@Qualifier
@Qualifier注解我们用来控制bean应在字段上自动装配。例如,具有两个类似的 person bean 配置文件
<!-- 通过<context:annotation-config />注册AutowiredAnnotationBeanPostProcessor -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<bean id="CustomerBean" class="com.gp6.autoAssemblyBean.autowired.qualifier.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean1" class="com.gp6.autoAssemblyBean.autowired.qualifier.Person">
<property name="name" value="gp6" />
<property name="address" value="address 123" />
<property name="age" value="28" />
</bean>
<bean id="PersonBean2" class="com.gp6.autoAssemblyBean.autowired.qualifier.Person">
<property name="name" value="gp6" />
<property name="address" value="address 123" />
<property name="age" value="28" />
</bean>
</beans>
Spring知道哪个 bean 应当装配?
为了解决这个问题,可以使用 @Qualifier 自动装配一个特定的 bean,例如,
package com.gp6.autoAssemblyBean.autowired.qualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Customer {
@Autowired
@Qualifier("PersonBean1")
private Person person;
private int type;
private String action;
//getter and setter methods
}
这意味着,“PersonBean1” bean被自动装配到customer的person属性。阅读下面完整的例子 – Spring自动装配@Qualifier实例
总结
@Autowired注解非常灵活,功能强大,绝对比bean配置文件的“autowire”属性要更好。