Spring(十):注解实现自动装配

打印 上一主题 下一主题

主题 873|帖子 873|积分 2619

上一篇文章我们已经学习了Bean的自动装配,是在xml文件中配置autowire来实现的,现在我们来学习一下通过注解来实现自动装配。
一、使用注解需要的准备工作
使用注解在xml配置文件中导入约束并配置对注解的支持:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>
复制代码
二、注解的使用
仍然是上一篇文章的三个类,People、Cat、Dog。
xml文件配置如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans><?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans><?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans><?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans><?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.     xmlns:context="http://www.springframework.org/schema/context"
  65.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  66.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  67.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  68.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  69.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  70.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  71.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.     xmlns:context="http://www.springframework.org/schema/context"
  77.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  79.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  80.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  81.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  82.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  83.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  84.     <context:annotation-config/>
  85. </beans>
复制代码
1.@Autowired
@Autowired注解
@Autowired注解是spring的一个注解,这个注解可以在属性、构造函数、set方法等地方使用,能够实现Bean的自动装配。
(1)在属性上使用
  1. public class People {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>private String name;<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans>@Autowired<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>private Dog dog;<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans>@Autowired<?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans>private Cat cat;}
复制代码
(2)在构造函数上使用
  1. public class People {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>private String name;<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans>private Dog dog;<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>private Cat cat;<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans>@Autowired<?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans>public People(Dog dog, Cat cat) {<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.     xmlns:context="http://www.springframework.org/schema/context"
  65.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  66.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  67.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  68.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  69.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  70.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  71.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.     xmlns:context="http://www.springframework.org/schema/context"
  77.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  79.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  80.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  81.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  82.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  83.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  84.     <context:annotation-config/>
  85. </beans>this.dog = dog;<?xml version="1.0" encoding="UTF-8"?>
  86. <beans xmlns="http://www.springframework.org/schema/beans"
  87.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.     xmlns:context="http://www.springframework.org/schema/context"
  89.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  90.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  91.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  92.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  93.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  94.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  95.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  96.     <context:annotation-config/>
  97. </beans><?xml version="1.0" encoding="UTF-8"?>
  98. <beans xmlns="http://www.springframework.org/schema/beans"
  99.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100.     xmlns:context="http://www.springframework.org/schema/context"
  101.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  102.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  103.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  104.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  105.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  106.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  107.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  108.     <context:annotation-config/>
  109. </beans>this.cat = cat;<?xml version="1.0" encoding="UTF-8"?>
  110. <beans xmlns="http://www.springframework.org/schema/beans"
  111.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  112.     xmlns:context="http://www.springframework.org/schema/context"
  113.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  114.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  115.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  116.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  117.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  118.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  119.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  120.     <context:annotation-config/>
  121. </beans>}
复制代码
(3)在set方法上使用
  1. public class People {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>private String name;<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans>private Dog dog;<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>private Cat cat;<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans>@Autowired<?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans>public void setDog(Dog dog) {<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.     xmlns:context="http://www.springframework.org/schema/context"
  65.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  66.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  67.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  68.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  69.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  70.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  71.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.     xmlns:context="http://www.springframework.org/schema/context"
  77.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  79.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  80.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  81.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  82.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  83.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  84.     <context:annotation-config/>
  85. </beans>this.dog = dog;<?xml version="1.0" encoding="UTF-8"?>
  86. <beans xmlns="http://www.springframework.org/schema/beans"
  87.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.     xmlns:context="http://www.springframework.org/schema/context"
  89.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  90.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  91.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  92.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  93.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  94.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  95.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  96.     <context:annotation-config/>
  97. </beans>}<?xml version="1.0" encoding="UTF-8"?>
  98. <beans xmlns="http://www.springframework.org/schema/beans"
  99.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100.     xmlns:context="http://www.springframework.org/schema/context"
  101.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  102.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  103.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  104.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  105.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  106.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  107.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  108.     <context:annotation-config/>
  109. </beans>@Autowired<?xml version="1.0" encoding="UTF-8"?>
  110. <beans xmlns="http://www.springframework.org/schema/beans"
  111.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  112.     xmlns:context="http://www.springframework.org/schema/context"
  113.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  114.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  115.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  116.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  117.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  118.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  119.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  120.     <context:annotation-config/>
  121. </beans>public void setCat(Cat cat) {<?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.     xmlns:context="http://www.springframework.org/schema/context"
  125.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  126.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  127.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  128.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  129.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  130.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  131.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  132.     <context:annotation-config/>
  133. </beans><?xml version="1.0" encoding="UTF-8"?>
  134. <beans xmlns="http://www.springframework.org/schema/beans"
  135.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  136.     xmlns:context="http://www.springframework.org/schema/context"
  137.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  138.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  139.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  140.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  141.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  142.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  143.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  144.     <context:annotation-config/>
  145. </beans>this.cat = cat;<?xml version="1.0" encoding="UTF-8"?>
  146. <beans xmlns="http://www.springframework.org/schema/beans"
  147.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  148.     xmlns:context="http://www.springframework.org/schema/context"
  149.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  150.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  151.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  152.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  153.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  154.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  155.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  156.     <context:annotation-config/>
  157. </beans>}}
复制代码
这三种方法进行测试得到的都可以获得最后的结果,测试如下:
  1. @Test<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>public void test1() {<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans><?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans><?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans>People people = applicationContext.getBean("people", People.class);<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.     xmlns:context="http://www.springframework.org/schema/context"
  65.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  66.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  67.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  68.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  69.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  70.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  71.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.     xmlns:context="http://www.springframework.org/schema/context"
  77.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  79.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  80.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  81.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  82.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  83.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  84.     <context:annotation-config/>
  85. </beans>System.out.println(people.getName());<?xml version="1.0" encoding="UTF-8"?>
  86. <beans xmlns="http://www.springframework.org/schema/beans"
  87.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.     xmlns:context="http://www.springframework.org/schema/context"
  89.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  90.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  91.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  92.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  93.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  94.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  95.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  96.     <context:annotation-config/>
  97. </beans><?xml version="1.0" encoding="UTF-8"?>
  98. <beans xmlns="http://www.springframework.org/schema/beans"
  99.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100.     xmlns:context="http://www.springframework.org/schema/context"
  101.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  102.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  103.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  104.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  105.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  106.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  107.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  108.     <context:annotation-config/>
  109. </beans>people.getDog().bark();<?xml version="1.0" encoding="UTF-8"?>
  110. <beans xmlns="http://www.springframework.org/schema/beans"
  111.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  112.     xmlns:context="http://www.springframework.org/schema/context"
  113.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  114.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  115.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  116.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  117.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  118.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  119.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  120.     <context:annotation-config/>
  121. </beans><?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.     xmlns:context="http://www.springframework.org/schema/context"
  125.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  126.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  127.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  128.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  129.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  130.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  131.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  132.     <context:annotation-config/>
  133. </beans>people.getCat().bark();<?xml version="1.0" encoding="UTF-8"?>
  134. <beans xmlns="http://www.springframework.org/schema/beans"
  135.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  136.     xmlns:context="http://www.springframework.org/schema/context"
  137.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  138.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  139.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  140.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  141.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  142.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  143.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  144.     <context:annotation-config/>
  145. </beans>}
复制代码
测试结果:

 
 
 除此之外@Autowired还有一个属性required默认为true,我们可以设置它为false,当我们设置了false时,被注解的值就可以为null,否则不行。

 
 
  1. public class People {<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>private String name;<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans>@Autowired<?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>private Dog dog;<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans>@Autowired(required = false)<?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans>private Cat cat;}
复制代码
删掉xml文件中cat的bean:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans><?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans><?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans><?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans><?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans><?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.     xmlns:context="http://www.springframework.org/schema/context"
  65.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  66.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  67.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  68.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  69.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  70.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  71.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans>
复制代码
测试:
  1. @Test<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>public void test1() {<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans><?xml version="1.0" encoding="UTF-8"?>
  26. <beans xmlns="http://www.springframework.org/schema/beans"
  27.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  28.     xmlns:context="http://www.springframework.org/schema/context"
  29.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  30.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  31.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  32.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  33.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  34.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  35.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  36.     <context:annotation-config/>
  37. </beans>ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");<?xml version="1.0" encoding="UTF-8"?>
  38. <beans xmlns="http://www.springframework.org/schema/beans"
  39.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  40.     xmlns:context="http://www.springframework.org/schema/context"
  41.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  42.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  43.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  44.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  45.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  46.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  47.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  48.     <context:annotation-config/>
  49. </beans><?xml version="1.0" encoding="UTF-8"?>
  50. <beans xmlns="http://www.springframework.org/schema/beans"
  51.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  52.     xmlns:context="http://www.springframework.org/schema/context"
  53.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  54.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  55.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  56.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  57.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  58.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  59.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  60.     <context:annotation-config/>
  61. </beans>People people = applicationContext.getBean("people", People.class);<?xml version="1.0" encoding="UTF-8"?>
  62. <beans xmlns="http://www.springframework.org/schema/beans"
  63.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  64.     xmlns:context="http://www.springframework.org/schema/context"
  65.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  66.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  67.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  68.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  69.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  70.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  71.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  72.     <context:annotation-config/>
  73. </beans><?xml version="1.0" encoding="UTF-8"?>
  74. <beans xmlns="http://www.springframework.org/schema/beans"
  75.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  76.     xmlns:context="http://www.springframework.org/schema/context"
  77.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  78.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  79.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  80.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  81.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  82.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  83.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  84.     <context:annotation-config/>
  85. </beans>System.out.println(people.getName());<?xml version="1.0" encoding="UTF-8"?>
  86. <beans xmlns="http://www.springframework.org/schema/beans"
  87.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  88.     xmlns:context="http://www.springframework.org/schema/context"
  89.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  90.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  91.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  92.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  93.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  94.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  95.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  96.     <context:annotation-config/>
  97. </beans><?xml version="1.0" encoding="UTF-8"?>
  98. <beans xmlns="http://www.springframework.org/schema/beans"
  99.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  100.     xmlns:context="http://www.springframework.org/schema/context"
  101.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  102.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  103.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  104.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  105.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  106.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  107.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  108.     <context:annotation-config/>
  109. </beans>people.getDog().bark();<?xml version="1.0" encoding="UTF-8"?>
  110. <beans xmlns="http://www.springframework.org/schema/beans"
  111.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  112.     xmlns:context="http://www.springframework.org/schema/context"
  113.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  114.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  115.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  116.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  117.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  118.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  119.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  120.     <context:annotation-config/>
  121. </beans><?xml version="1.0" encoding="UTF-8"?>
  122. <beans xmlns="http://www.springframework.org/schema/beans"
  123.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  124.     xmlns:context="http://www.springframework.org/schema/context"
  125.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  126.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  127.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  128.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  129.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  130.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  131.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  132.     <context:annotation-config/>
  133. </beans>System.out.println(people.getCat());<?xml version="1.0" encoding="UTF-8"?>
  134. <beans xmlns="http://www.springframework.org/schema/beans"
  135.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  136.     xmlns:context="http://www.springframework.org/schema/context"
  137.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  138.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  139.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  140.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  141.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  142.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  143.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  144.     <context:annotation-config/>
  145. </beans>}
复制代码
测试结果:

 
 
 从测试结果可以看到,cat变成了null。
此时,我们了解了@Autowired的基本用法,我们来思考一个问题,如果有多个同类型的bean会发生什么呢?此时我们修改Cat类型的bean为两个:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans><?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans>
复制代码

 
结果报错了,它会让我们去选择bean。上面很明显两个bean的id都和属性名不同,如果让其中一个id与属性名相同能否找到呢?
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans><?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans>
复制代码
不再报错。
如果属性名与字段名不同,能否将上面的问题解决呢?答案是可以的,我们需要用到@Qualifier(value = ""),我们只需要让value等于bean的id即可。
如下面的示例:
  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>
复制代码
  1.   @Autowired<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>@Qualifier(value = "cat1")<?xml version="1.0" encoding="UTF-8"?>
  14. <beans xmlns="http://www.springframework.org/schema/beans"
  15.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  16.     xmlns:context="http://www.springframework.org/schema/context"
  17.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  18.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  19.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  20.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  21.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  22.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  23.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  24.     <context:annotation-config/>
  25. </beans>private Cat cat;
复制代码
总结一下:
@Autowired注解是先去IOC容器中查到对应类型的Bean,如果有多个对应类型的Bean,然后根据Bean的id进行匹配。
@Autowired有一个属性required默认为true,我们可以设置它为false。true意味着我们必须找到Bean,false意味着可以找不到Bean(null)。
@Autowired可以@Qualifier(value = "")配合使用,这样就会去容器中找id与value值匹配的Bean。并且此时即便只有一个相同类型的Bean,如果id不匹配也会报错。
2.@Resource注解
@Resource注解是java中的一个注解,使用它同样可以实现Bean的自动装配。
@Resource有一个name属性,当使用name属性时,会根据name的值匹配Bean的id。
下面是使用的几个例子:
(1)不使用name属性,唯一Bean没有id
  1. @Resource<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>private Dog dog;
复制代码
  1. [/code]没有问题。即使有id且id随意,也是能够找到Bean的,因为类型匹配且唯一。
  2. (2)不使用name属性,Bean不唯一但有能够匹配属性名的id
  3. [code]  <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6.     xmlns:context="http://www.springframework.org/schema/context"
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  12.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  13.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  14.     <context:annotation-config/>
  15. </beans>
复制代码
这种情况也没有问题,因为id能够与属性名匹配。
(3)使用name属性,Bean不唯一但有能匹配name的id
  1. @Resource(name = "dog1")<?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>private Dog dog;
复制代码
  1.   <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  7.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/beans/spring-beans.xsd
  8.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  9.     <bean id="cat1" class="com.jms.pojo.Cat"/>http://www.springframework.org/schema/context
  10.     <bean id="cat2" class="com.jms.pojo.Cat"/>
  11.     <bean id="cat1" class="com.jms.pojo.Cat"/>https://www.springframework.org/schema/context/spring-context.xsd">
  12.     <context:annotation-config/>
  13. </beans>
复制代码
没有问题,因为name能与Bean的id匹配。
总而言之,@Resource与@Autowried用法基本相同,都是先去容器中匹配类型,如过有多个相同类型则将属性名与id进行匹配;当使用了name或者@Qualifier(value = "")时,会直接去对应类型的Bean中找id能与name或value值相匹配的,如果找不到,即便类型匹配且唯一也会报错。
我们用一个图来更加明了的看一下整个流程(以@Autowried为例):

 
 
 
(本文仅作个人学习记录用,如有纰漏敬请指正)
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表