---
title: "Spring2.5的新特性—Autowired实现快速的自动注入"
author: "Openhearted"
date: "2010-08-01T23:38:30Z"
tags: []
categories: ["企业架构"]
slug: "spring2-5-autowired"
draft: false
lastmod: "2010-08-01T23:38:30Z"
id: 726736
url: "https://openhearted.iteye.com/blog/726736"
---

使用Spring2.5的新特性——Autowired可以实现快速的自动注入，而无需在xml文档里面添加bean的声明，大大减少了xml文档的维护。（偶喜欢这个功能，因为偶对xml不感冒）。      

以下是一个例子：  

先编写接口Man：  

   1. public interface Man {        
   2.     
   3.       public String sayHello();        
   4.     
   5. }      

然后写Man的实现类Chinese和American：  

   1. @Service       
   2.     
   3. public class Chinese implements Man{        
   4.     
   5.     public String sayHello() {        
   6.     
   7.         return "I am Chinese!";        
   8.     
   9.     }        
  10.     
  11. }        
  12.     
  13.        
  14.     
  15. @Service       
  16.     
  17. public class American implements Man{        
  18.     
  19.     public String sayHello() {        
  20.     
  21.         return "I am American!";        
  22.     
  23.     }        
  24.     
  25. }        
  26.   

      @Service注释表示定义一个bean，自动根据bean的类名实例化一个首写字母为小写的bean，例如Chinese实例化为 chinese，American实例化为american，如果需要自己改名字则:@Service("你自己改的bean名")。  

beans.xml  

   1. &lt;?xml version="1.0" encoding="UTF-8"?&gt;        
   2.     
   3. &lt;beans xmlns="http://www.springframework.org/schema/beans"       
   4.     
   5.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
   6.     
   7.         xmlns:context="http://www.springframework.org/schema/context"       
   8.     
   9.         xsi:schemaLocation="http://www.springframework.org/schema/beans         
  10.     
  11.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        
  12.     
  13.            http://www.springframework.org/schema/context        
  14.     
  15.            http://www.springframework.org/schema/context/spring-context-2.5.xsd"&gt;        
  16.     
  17.       &lt;context:annotation-config/&gt;        
  18.     
  19.       &lt;context:component-scan base-package="testspring.main"/&gt;        
  20.     
  21. &lt;/beans&gt;        
  22.   

在spring的配置文件里面只需要加上&lt;context:annotation-config/&gt; 和&lt;context:component-scan base-package="需要实现注入的类所在包"/&gt;，可以使用base-package="\*"表示全部的类。  

编写主类测试：  

   1. @Service       
   2.     
   3. public class Main {        
   4.     
   5.     @Autowired       
   6.     
   7.     @Qualifier("chinese")        
   8.     
   9.     private Man man;        
  10.     
  11.        
  12.     
  13.     public static void main(String\[] args) {        
  14.     
  15.         // TODO code application logic here        
  16.     
  17.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");        
  18.     
  19.         Main main = (Main) ctx.getBean("main");        
  20.     
  21.         System.out.println(main.getMan().sayHello());        
  22.     
  23.     }        
  24.     
  25.        
  26.     
  27.     public Man getMan() {        
  28.     
  29.         return man;        
  30.     
  31.     }        
  32.     
  33. }        
  34.   

在Man接口前面标上@Autowired和@Qualifier注释使得Man接口可以被容器注入，当Man接口存在两个实现类的时候必须指定其中一个来注入，使用实现类首字母小写的字符串来注入。否则可以省略，只写@Autowired   

 

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  

使用 Spring 2.5 注释驱动的 IoC 功能  

注释配置相对于 XML 配置具有很多的优势：  

它可以充分利用 Java 的反射机制获取类结构信息，这些信息可以有效减少配置的工作。如使用 JPA 注释配置 ORM 映射时，我们就不需要指定 PO 的属性名、类型等信息，如果关系表字段和 PO 属性名、类型都一致，您甚至无需编写任务属性映射信息——因为这些信息都可以通过 Java 反射机制获取。   

注释和 Java 代码位于一个文件中，而 XML 配置采用独立的配置文件，大多数配置信息在程序开发完成后都不会调整，如果配置信息和 Java 代码放在一起，有助于增强程序的内聚性。而采用独立的 XML 配置文件，程序员在编写一个功能时，往往需要在程序文件和配置文件中不停切换，这种思维上的不连贯会降低开发效率。   

因此在很多情况下，注释配置比 XML 配置更受欢迎，注释配置有进一步流行的趋势。Spring 2.5 的一大增强就是引入了很多注释类，现在您已经可以使用注释配置完成大部分 XML 配置的功能。在这篇文章里，我们将向您讲述使用注释进行 Bean 定义和依赖注入的内容。  

原来我们是怎么做的  

在使用注释配置之前，先来回顾一下传统上是如何配置 Bean 并完成 Bean 之间依赖关系的建立。下面是 3 个类，它们分别是 Office、Car 和 Boss，这 3 个类需要在 Spring 容器中配置为 Bean：  

Office 仅有一个属性：  

清单 1. Office.java  

   1. package com.baobaotao;        
   2.     
   3. public class Office {        
   4.     
   5.     private String officeNo =”001”;        
   6.     
   7.        
   8.     
   9.     //省略 get /setter        
  10.     
  11.        
  12.     
  13.     @Override       
  14.     
  15.     public String toString() {        
  16.     
  17.         return "officeNo:" + officeNo;        
  18.     
  19.     }        
  20.     
  21. }        
  22.   

 

Car 拥有两个属性：  

清单 2. Car.java          

   1. package com.baobaotao;        
   2.     
   3. public class Car {        
   4.     
   5.     private String brand;        
   6.     
   7.     private double price;        
   8.     
   9.      // 省略 get /setter        
  10.     
  11.       @Override       
  12.     
  13.     public String toString() {        
  14.     
  15.         return "brand:" + brand + "," + "price:" + price;        
  16.     
  17.     }        
  18.     
  19. }        
  20.   

 

Boss 拥有 Office 和 Car 类型的两个属性：  

清单 3. Boss.java  

   1. package com.baobaotao;        
   2.     
   3. public class Boss {        
   4.     
   5.     private Car car;        
   6.     
   7.     private Office office;        
   8.     
   9.      // 省略 get /setter        
  10.     
  11.      @Override       
  12.     
  13.     public String toString() {        
  14.     
  15.         return "car:" + car + "\\n" + "office:" + office;        
  16.     
  17.     }        
  18.     
  19. }        
  20.   

我们在 Spring 容器中将 Office 和 Car 声明为 Bean，并注入到 Boss Bean 中：下面是使用传统 XML 完成这个工作的配置文件 beans.xml：  

清单 4. beans.xml 将以上三个类配置成 Bean  

   1. &lt;?xml version="1.0" encoding="UTF-8" ?&gt;        
   2.     
   3. &lt;beans xmlns="http://www.springframework.org/schema/beans"       
   4.     
   5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
   6.     
   7.     xsi:schemaLocation="http://www.springframework.org/schema/beans         
   8.     
   9.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;        
  10.     
  11.     &lt;bean id="boss" class="com.baobaotao.Boss"&gt;        
  12.     
  13.         &lt;property name="car" ref="car"/&gt;        
  14.     
  15.         &lt;property name="office" ref="office" /&gt;        
  16.     
  17.     &lt;/bean&gt;        
  18.     
  19.     &lt;bean id="office" class="com.baobaotao.Office"&gt;        
  20.     
  21.         &lt;property name="officeNo" value="002"/&gt;        
  22.     
  23.     &lt;/bean&gt;        
  24.     
  25.     &lt;bean id="car" class="com.baobaotao.Car" scope="singleton"&gt;        
  26.     
  27.         &lt;property name="brand" value=" 红旗 CA72"/&gt;        
  28.     
  29.         &lt;property name="price" value="2000"/&gt;        
  30.     
  31.     &lt;/bean&gt;        
  32.     
  33. &lt;/beans&gt;        
  34.   

 

当我们运行以下代码时，控制台将正确打出 boss 的信息：  

清单 5. 测试类：AnnoIoCTest.java    

   1. import org.springframework.context.ApplicationContext;        
   2.     
   3. import org.springframework.context.support.ClassPathXmlApplicationContext;        
   4.     
   5. public class AnnoIoCTest {        
   6.     
   7.        
   8.     
   9.     public static void main(String\[] args) {        
  10.     
  11.         String\[] locations = {"beans.xml"};        
  12.     
  13.         ApplicationContext ctx =         
  14.     
  15.             new ClassPathXmlApplicationContext(locations);        
  16.     
  17.         Boss boss = (Boss) ctx.getBean("boss");        
  18.     
  19.         System.out.println(boss);        
  20.     
  21.     }        
  22.     
  23. }        
  24.   

这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。  

 

使用 @Autowired 注释  

Spring 2.5 引入了 @Autowired 注释，它可以对类成员变量、方法及构造函数进行标注，完成自动装配的工作。来看一下使用 @Autowired 进行成员变量自动注入的代码：  

清单 6. 使用 @Autowired 注释的 Boss.java  

   1. package com.baobaotao;        
   2.     
   3. import org.springframework.beans.factory.annotation.Autowired;        
   4.     
   5.     
   6. public class Boss {        
   7.     
   8.     
   9.     @Autowired       
  10.     
  11.     private Car car;        
  12.        
  13.     
  14.     @Autowired       
  15.     
  16.     private Office office;        
  17.     
  18.        
  19.     
  20.     …        
  21.     
  22. }        
  23.   

Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析，所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。  

清单 7. 让 @Autowired 注释工作起来  

   1. &lt;?xml version="1.0" encoding="UTF-8" ?&gt;        
   2.     
   3. &lt;beans xmlns="http://www.springframework.org/schema/beans"       
   4.     
   5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
   6.     
   7.     xsi:schemaLocation="http://www.springframework.org/schema/beans         
   8.     
   9.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;        
  10.     
  11.       &lt;!-- 该 BeanPostProcessor 将自动起作用，对标注 @Autowired 的 Bean 进行自动注入 --&gt;        
  12.     
  13.     &lt;bean class="org.springframework.beans.factory.annotation.        
  14.     
  15.         AutowiredAnnotationBeanPostProcessor"/&gt;        
  16.     
  17.      &lt;!-- 移除 boss Bean 的属性注入配置的信息 --&gt;        
  18.     
  19.     &lt;bean id="boss" class="com.baobaotao.Boss"/&gt;        
  20.     
  21.        &lt;bean id="office" class="com.baobaotao.Office"&gt;        
  22.     
  23.         &lt;property name="officeNo" value="001"/&gt;        
  24.     
  25.     &lt;/bean&gt;        
  26.     
  27.     &lt;bean id="car" class="com.baobaotao.Car" scope="singleton"&gt;        
  28.     
  29.         &lt;property name="brand" value=" 红旗 CA72"/&gt;        
  30.     
  31.         &lt;property name="price" value="2000"/&gt;        
  32.     
  33.     &lt;/bean&gt;        
  34.     
  35. &lt;/beans&gt;        
  36.   

这样，当 Spring 容器启动时，AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean，当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配（默认按类型匹配）的 Bean，并注入到对应的地方中去。  

按照上面的配置，Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后，您大可将它们的 setter 方法（setCar() 和 setOffice()）从 Boss 中删除。  

 当然，您也可以通过 @Autowired 对方法或构造函数进行标注，来看下面的代码：  

清单 8. 将 @Autowired 注释标注在 Setter 方法上  

   1. package com.baobaotao;        
   2.     
   3. public class Boss {        
   4.     
   5.     private Car car;        
   6.     
   7.     private Office office;        
   8.     
   9.        
  10.     
  11.      @Autowired       
  12.     
  13.     public void setCar(Car car) {        
  14.     
  15.         this.car = car;        
  16.     
  17.     }        
  18.     
  19.          
  20.     
  21.     @Autowired       
  22.     
  23.     public void setOffice(Office office) {        
  24.     
  25.         this.office = office;        
  26.     
  27.     }        
  28.     
  29.     …        
  30.     
  31. }        
  32.   

这时，@Autowired 将查找被标注的方法的入参类型的 Bean，并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注：  

 清单 9. 将 @Autowired 注释标注在构造函数上         

   1. package com.baobaotao;        
   2.     
   3.   public class Boss {        
   4.     
   5.     private Car car;        
   6.     
   7.     private Office office;        
   8.     
   9.          
  10.     
  11.     @Autowired       
  12.     
  13.     public Boss(Car car ,Office office){        
  14.     
  15.         this.car = car;        
  16.     
  17.         this.office = office ;        
  18.     
  19.     }        
  20.     
  21.          
  22.     
  23.     …        
  24.     
  25. }        
  26.   

由于 Boss() 构造函数有两个入参，分别是 car 和 office，@Autowired 将分别寻找和它们类型匹配的 Bean，将它们作为 Boss(Car car ,Office office) 的入参来创建 Boss Bean。  

 

当候选 Bean 数目不为 1 时的应对方法  

在默认情况下使用 @Autowired 注释进行自动注入时，Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时，Spring 容器将抛出 BeanCreationException 异常，并指出必须至少拥有一个匹配的 Bean。我们可以来做一个实验：  

清单 10. 候选 Bean 数目为 0 时  

   1. &lt;?xml version="1.0" encoding="UTF-8" ?&gt;        
   2.     
   3. &lt;beans xmlns="http://www.springframework.org/schema/beans"       
   4.     
   5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
   6.     
   7.      xsi:schemaLocation="http://www.springframework.org/schema/beans         
   8.     
   9.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd "&gt;        
  10.     
  11.          
  12.     
  13.     &lt;bean class="org.springframework.beans.factory.annotation.        
  14.     
  15.         AutowiredAnnotationBeanPostProcessor"/&gt;         
  16.     
  17.        
  18.     
  19.     &lt;bean id="boss" class="com.baobaotao.Boss"/&gt;        
  20.     
  21.        
  22.     
  23.     &lt;!-- 将 office Bean 注释掉 --&gt;        
  24.     
  25.     &lt;!-- &lt;bean id="office" class="com.baobaotao.Office"&gt;        
  26.     
  27.     &lt;property name="officeNo" value="001"/&gt;        
  28.     
  29.     &lt;/bean&gt;--&gt;        
  30.     
  31.        
  32.     
  33.     &lt;bean id="car" class="com.baobaotao.Car" scope="singleton"&gt;        
  34.     
  35.         &lt;property name="brand" value=" 红旗 CA72"/&gt;        
  36.     
  37.         &lt;property name="price" value="2000"/&gt;        
  38.     
  39.     &lt;/bean&gt;        
  40.     
  41. &lt;/beans&gt;        
  42.   

由于 office Bean 被注释掉了，所以 Spring 容器中将没有类型为 Office 的 Bean 了，而 Boss 的 office 属性标注了 @Autowired，当启动 Spring 容器时，异常就产生了。  

 

当不能确定 Spring 容器中一定拥有某个类的 Bean 时，可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false)，这等于告诉 Spring：在找不到匹配 Bean 时也不报错。来看一下具体的例子：