注 @Autowired注解是通过匹配数据类型自动装配Bean

    1. Beans

    2.注册AutowiredAnnotationBeanPostProcessor
    要启用@Autowired,必须注册“AutowiredAnnotationBeanPostProcessor’,你可以用两种方式做到这一点:

    1. 1. Include <context:annotation-config
    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans
    5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. http://www.springframework.org/schema/context
    7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    8. <context:annotation-config />
    9. <bean id="CustomerBean" class="com.yiibai.common.Customer">
    10. <property name="action" value="buy" />
    11. <property name="type" value="1" />
    12. </bean>
    13. <bean id="PersonBean" class="com.yiibai.common.Person">
    14. <property name="name" value="yiibai" />
    15. <property name="address" value="address ABC" />
    16. <property name="age" value="29" />
    17. </bean>
    18. </beans>
    1. 包含 AutowiredAnnotationBeanPostProcessor
    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://www.springframework.org/schema/beans
    4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    5. <bean
    6. class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    7. <bean id="CustomerBean" class="com.yiibai.common.Customer">
    8. <property name="action" value="buy" />
    9. <property name="type" value="1" />
    10. </bean>
    11. <bean id="PersonBean" class="com.yiibai.common.Person">
    12. <property name="name" value="yiibai" />
    13. <property name="address" value="address ABC" />
    14. <property name="age" value="29" />
    15. </bean>
    16. </beans>
    1. @Autowired示例
    1. 1. @Autowired setter 方法
    2. package com.yiibai.common;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. public class Customer
    5. {
    6. private Person person;
    7. private int type;
    8. private String action;
    9. //getter and setter methods
    10. @Autowired
    11. public void setPerson(Person person) {
    12. this.person = person;
    13. }
    14. }
    15. 2. @Autowired 构造方法
    16. package com.yiibai.common;
    17. import org.springframework.beans.factory.annotation.Autowired;
    18. public class Customer
    19. {
    20. private Person person;
    21. private int type;
    22. private String action;
    23. //getter and setter methods
    24. @Autowired
    25. public Customer(Person person) {
    26. this.person = person;
    27. }
    28. }
    29. 3. @Autowired 字段
    30. package com.yiibai.common;
    31. import org.springframework.beans.factory.annotation.Autowired;
    32. public class Customer
    33. {
    34. @Autowired
    35. private Person person;
    36. private int type;
    37. private String action;
    38. //getter and setter methods
    39. }

    依赖检查

    1. 要解决这个问题,可以通过 @Autowired 的“required”属性设置为false来禁用此检查功能。
    2. package com.yiibai.common;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. public class Customer
    5. {
    6. @Autowired(required=false)
    7. private Person person;
    8. private int type;
    9. private String action;
    10. //getter and setter methods
    11. }

    @Qualifier

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans
    5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    6. http://www.springframework.org/schema/context
    7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    8. <context:annotation-config />
    9. <bean id="CustomerBean" class="com.yiibai.common.Customer">
    10. <property name="action" value="buy" />
    11. <property name="type" value="1" />
    12. </bean>
    13. <bean id="PersonBean1" class="com.yiibai.common.Person">
    14. <property name="name" value="yiibai-1" />
    15. <property name="address" value="address-1" />
    16. <property name="age" value="29" />
    17. </bean>
    18. <bean id="PersonBean2" class="com.yiibai.common.Person">
    19. <property name="name" value="yiibai-2" />
    20. <property name="address" value="address-2" />
    21. <property name="age" value="28" />
    22. </bean>
    23. </beans>
    1. 可以使用 @Qualifier 自动装配一个特定的 bean
    2. package com.yiibai.common;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.beans.factory.annotation.Qualifier;
    5. public class Customer
    6. {
    7. @Autowired
    8. @Qualifier("PersonBean1")
    9. private Person person;
    10. private int type;
    11. private String action;
    12. //getter and setter methods
    13. }