注 @Autowired注解是通过匹配数据类型自动装配Bean
- Beans
2.注册AutowiredAnnotationBeanPostProcessor
要启用@Autowired,必须注册“AutowiredAnnotationBeanPostProcessor’,你可以用两种方式做到这一点:
1. Include <context:annotation-config
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans>
- 包含 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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><beanclass="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans>
- @Autowired示例
1. @Autowired setter 方法package com.yiibai.common;import org.springframework.beans.factory.annotation.Autowired;public class Customer{private Person person;private int type;private String action;//getter and setter methods@Autowiredpublic void setPerson(Person person) {this.person = person;}}2. @Autowired 构造方法package com.yiibai.common;import org.springframework.beans.factory.annotation.Autowired;public class Customer{private Person person;private int type;private String action;//getter and setter methods@Autowiredpublic Customer(Person person) {this.person = person;}}3. @Autowired 字段package com.yiibai.common;import org.springframework.beans.factory.annotation.Autowired;public class Customer{@Autowiredprivate Person person;private int type;private String action;//getter and setter methods}
依赖检查
要解决这个问题,可以通过 @Autowired 的“required”属性设置为false来禁用此检查功能。package com.yiibai.common;import org.springframework.beans.factory.annotation.Autowired;public class Customer{@Autowired(required=false)private Person person;private int type;private String action;//getter and setter methods}
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean1" class="com.yiibai.common.Person"><property name="name" value="yiibai-1" /><property name="address" value="address-1" /><property name="age" value="29" /></bean><bean id="PersonBean2" class="com.yiibai.common.Person"><property name="name" value="yiibai-2" /><property name="address" value="address-2" /><property name="age" value="28" /></bean></beans>
可以使用 @Qualifier 自动装配一个特定的 beanpackage com.yiibai.common;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}
