读书人

Autowired兑现快速的自动注入

发布时间: 2012-11-06 14:07:00 作者: rapoo

Autowired实现快速的自动注入

清单 11. 使用 @Autowired(required = false) ?

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

??? 当然,一般情况下,使用 @Autowired 的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。 ?

?

和找不到一个类型匹配 Bean 相反的一个错误是:如果 Spring 容器中拥有多个候选 Bean,Spring 容器在启动时也会抛出 BeanCreationException 异常。来看下面的例子: ?

清单 12. 在 beans.xml 中配置两个 Office 类型的 Bean???????? ?

?? 1. …????? ?
?? 2.? ?
?? 3. <bean id="office" value="001"/>???? ?
?? 6.? ?
?? 7. </bean>???? ?
?? 8.? ?
?? 9. <bean id="office2" value="001"/>???? ?
? 12.? ?
? 13. </bean>???? ?
? 14.? ?
? 15. …???? ?
? 16.? ?

我们在 Spring 容器中配置了两个类型为 Office 类型的 Bean,当对 Boss 的 office 成员变量进行自动注入时,Spring 容器将无法确定到底要用哪一个 Bean,因此异常发生了。 ?

?

Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常: ?

清单 13. 使用 @Qualifier 注释指定注入 Bean 的名称 ?

?? 1. @Autowired??? ?
?? 2.? ?
?? 3. public void setOffice(@Qualifier("office")Office office) {???? ?
?? 4.? ?
?? 5.???? this.office = office;???? ?
?? 6.? ?
?? 7. }??? ?

?@Qualifier("office") 中的 office 是 Bean 的名称,所以 @Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以 Spring 不将 @Autowired 和 @Qualifier 统一成一个注释类。下面是对成员变量和构造函数入参进行注释的代码: ?

? 对成员变量进行注释: ?

? 清单 14. 对成员变量使用 @Qualifier 注释???????????? ?

?? 1. public class Boss {???? ?
?? 2.? ?
?? 3.???? @Autowired??? ?
?? 4.? ?
?? 5.???? private Car car;???? ?
?? 6.? ?
?? 7.?????? ?
?? 8.? ?
?? 9.???? @Autowired??? ?
? 10.? ?
? 11.???? @Qualifier("office")???? ?
? 12.? ?
? 13.???? private Office office;???? ?
? 14.? ?
? 15.???? …???? ?
? 16.? ?
? 17. }???? ?
? 18.? ?

对构造函数入参进行注释: ?

清单 15. 对构造函数变量使用 @Qualifier 注释 ?

?? 1. public class Boss {???? ?
?? 2.? ?
?? 3.???? private Car car;???? ?
?? 4.? ?
?? 5.???? private Office office;???? ?
?? 6.? ?
?? 7.???? ?
?? 8.? ?
?? 9.???? @Autowired??? ?
? 10.? ?
? 11.???? public Boss(Car car , @Qualifier("office")Office office){???? ?
? 12.? ?
? 13.???????? this.car = car;???? ?
? 14.? ?
? 15.???????? this.office = office ;???? ?
? 16.? ?
? 17.???? }???? ?
? 18.? ?
? 19. }???? ?
? 20.? ?

@Qualifier 只能和 @Autowired 结合使用,是对 @Autowired 有益的补充。一般来讲,@Qualifier 对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。 ?

?

使用 JSR-250 的注释 ?

Spring 不但支持自己定义的 @Autowired 的注释,还支持几个由 JSR-250 规范定义的注释,它们分别是 @Resource、@PostConstruct 以及 @PreDestroy。 ?

@Resource

@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面 @Resource 默认按 byName 自动注入罢了。@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。 ?

Resource 注释类位于 Spring 发布包的 lib/j2ee/common-annotations.jar 类包中,因此在使用之前必须将其加入到项目的类库中。来看一个使用 @Resource 的例子: ?

?清单 16. 使用 @Resource 注释的 Boss.java?????? ?

?? 1. package com.baobaotao;???? ?
?? 2.? ?
?? 3. import javax.annotation.Resource;???? ?
?? 4.? ?
?? 5. public class Boss {???? ?
?? 6.? ?
?? 7.???? // 自动注入类型为 Car 的 Bean???? ?
?? 8.? ?
?? 9.???? @Resource??? ?
? 10.? ?
? 11.???? private Car car;???? ?
? 12.? ?
? 13.???? ?
? 14.? ?
? 15.???? // 自动注入 bean 名称为 office 的 Bean???? ?
? 16.? ?
? 17.???? @Resource(name = "office")???? ?
? 18.? ?
? 19.???? private Office office;???? ?
? 20.? ?
? 21. }???? ?
? 22.? ?

一般情况下,我们无需使用类似于 @Resource(type=Car.class) 的注释方式,因为 Bean 的类型信息可以通过 Java 反射从代码中获取。 ?

?

要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor: ?

?? 1. <bean??? encoding="UTF-8" ?>???? ?
?? 2.? ?
?? 3. <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">???? ?
? 16.? ?
? 17.?????? ?
? 18.? ?
? 19.???? <context:annotation-config/>????? ?
? 20.? ?
? 21.???? ?
? 22.? ?
? 23.???? <bean id="boss" value="001"/>???? ?
? 28.? ?
? 29.???? </bean>???? ?
? 30.? ?
? 31.???? <bean id="car" scope="singleton">???? ?
? 32.? ?
? 33.???????? <property name="brand" value=" 红旗 CA72"/>???? ?
? 34.? ?
? 35.???????? <property name="price" value="2000"/>???? ?
? 36.? ?
? 37.???? </bean>???? ?
? 38.? ?
? 39. </beans>???? ?
? 40.? ?

<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。? ?

在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。 ?

?

使用 @Component

? 虽然我们可以通过 @Autowired 或 @Resource 在 Bean 类中使用自动注入功能,但是 Bean 还是在 XML 文件中通过 <bean> 进行定义 —— 也就是说,在 XML 配置文件中定义 Bean,通过 @Autowired 或 @Resource 为 Bean 的成员变量、方法入参或构造函数入参提供自动注入的功能。能否也通过注释定义 Bean,从 XML 配置文件中完全移除 Bean 定义的配置呢?答案是肯定的,我们通过 Spring 2.5 提供的 @Component 注释就可以达到这个目标了。 ?

? 下面,我们完全使用注释定义 Bean 并完成 Bean 之间装配: ?

清单 20. 使用 @Component 注释的 Car.java???????? ?

?? 1. package com.baobaotao;???? ?
?? 2.? ?
?? 3.?? import org.springframework.stereotype.Component;???? ?
?? 4.? ?
?? 5.?? @Component??? ?
?? 6.? ?
?? 7. public class Car {???? ?
?? 8.? ?
?? 9.???? …???? ?
? 10.? ?
? 11. }???? ?

仅需要在类定义处,使用 @Component 注释就可以将一个类定义了 Spring 容器中的 Bean。下面的代码将 Office 定义为一个 Bean: ?

?

清单 21. 使用 @Component 注释的 Office.java?????????????????? ?

?? 1. package com.baobaotao;???? ?
?? 2.? ?
?? 3.? import org.springframework.stereotype.Component;???? ?
?? 4.? ?
?? 5.???? ?
?? 6.? ?
?? 7. @Component??? ?
?? 8.? ?
?? 9. public class Office {???? ?
? 10.? ?
? 11.???? private String officeNo = "001";???? ?
? 12.? ?
? 13.???? …???? ?
? 14.? ?
? 15. }?? ?
? 16.? ?

这样,我们就可以在 Boss 类中通过 @Autowired 注入前面定义的 Car 和 Office Bean 了。 ?

清单 22. 使用 @Component 注释的 Boss.java ?

?? 1. package com.baobaotao;???? ?
?? 2.? ?
?? 3. import org.springframework.beans.factory.annotation.Autowired;???? ?
?? 4.? ?
?? 5. import org.springframework.beans.factory.annotation.Required;???? ?
?? 6.? ?
?? 7. import org.springframework.beans.factory.annotation.Qualifier;???? ?
?? 8.? ?
?? 9. import org.springframework.stereotype.Component;???? ?
? 10.? ?
? 11.???? ?
? 12.? ?
? 13. @Component("boss")???? ?
? 14.? ?
? 15. public class Boss {???? ?
? 16.? ?
? 17.???? @Autowired??? ?
? 18.? ?
? 19.???? private Car car;???? ?
? 20.? ?
? 21.???? ?
? 22.? ?
? 23.???? @Autowired??? ?
? 24.? ?
? 25.???? private Office office;???? ?
? 26.? ?
? 27.???? …???? ?
? 28.? ?
? 29. }???? ?
? 30.? ?

@Component 有一个可选的入参,用于指定 Bean 的名称,在 Boss 中,我们就将 Bean 名称定义为“boss”。一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。 ?

? 在使用 @Component 注释后,Spring 容器必须启用类扫描机制以启用注释驱动 Bean 定义和注释驱动 Bean 自动注入的策略。Spring 2.5 对 context 命名空间进行了扩展,提供了这一功能,请看下面的配置: ?

清单 23. 简化版的 beans.xml??????????? ?

?? 1. <?xml version="1.0" encoding="UTF-8" ?>???? ?
?? 2.? ?
?? 3. <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">???? ?
? 16.? ?
? 17.???? <context:component-scan base-package="com.baobaotao"/>???? ?
? 18.? ?
? 19. </beans>???? ?
? 20.? ?

这里,所有通过 <bean> 元素定义 Bean 的配置内容已经被移除,仅需要添加一行 <context:component-scan/> 配置就解决所有问题了——Spring XML 配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 ?

?

<context:component-scan/> 还允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式,通过下表说明: ?

表 1. 扫描过滤方式 ?

过滤器类型 说明? ?

注释 假如 com.baobaotao.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。? ?

类名指定 通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。? ?

正则表达式 通过正则表达式定义过滤的类,如下所示: com\.baobaotao\.Default.*? ?

AspectJ 表达式 通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+? ?

下面是一个简单的例子: ?

?? 1. <context:component-scan base-package="com.baobaotao">???? ?
?? 2.? ?
?? 3.???? <context:include-filter type="regex"????? ?
?? 4.? ?
?? 5.???????? expression="com\.baobaotao\.service\..*"/>???? ?
?? 6.? ?
?? 7.???? <context:exclude-filter type="aspectj"????? ?
?? 8.? ?
?? 9.???????? expression="com.baobaotao.util..*"/>???? ?
? 10.? ?
? 11. </context:component-scan>???? ?
? 12.? ?

值得注意的是 <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。 ?

?

默认情况下通过 @Component 定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过 @Scope 注释来达到目标,如以下代码所示: ?

清单 24. 通过 @Scope 指定 Bean 的作用范围 ?

?? 1. package com.baobaotao;???? ?
?? 2.? ?
?? 3. import org.springframework.context.annotation.Scope;???? ?
?? 4.? ?
?? 5. …???? ?
?? 6.? ?
?? 7. @Scope("prototype")???? ?
?? 8.? ?
?? 9. @Component("boss")???? ?
? 10.? ?
? 11. public class Boss {???? ?
? 12.? ?
? 13.???? …???? ?
? 14.? ?
? 15. }???? ?
? 16.? ?

这样,当从 Spring 容器中获取 boss Bean 时,每次返回的都是新的实例了。 ?

?

采用具有特殊语义的注释 ?

Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。 ?

?

注释配置和 XML 配置的适用场合 ?

是否有了这些 IOC 注释,我们就可以完全摒除原来 XML 配置的方式呢?答案是否定的。有以下几点原因: ?

注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。? ?

如果 Bean 不是自己编写的类(如 JdbcTemplate、SessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。? ?

注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于 @Transaction 事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。? ?

所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。 ?

?

小结 ?

? Spring 在 2.1 以后对注释配置提供了强力的支持,注释配置功能成为 Spring 2.5 的最大的亮点之一。合理地使用 Spring 2.5 的注释配置,可以有效减少配置的工作量,提高程序的内聚性。但是这并不意味着传统 XML 配置将走向消亡,在第三方类 Bean 的配置,以及那些诸如数据源、缓存池、持久层操作模板类、事务管理等内容的配置上,XML 配置依然拥有不可替代的地位。

读书人网 >软件架构设计

热点推荐