转:<context:annotation-config/>

来源:互联网 发布:知乎海尔收购通用电器 编辑:程序博客网 时间:2024/06/11 18:11

使用<context:annotation-config/>简化配置

Spring 2.1 添加了一个新的 context 的 Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。

而我们前面所介绍的AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring为我们提供了一种方便的注册这些BeanPostProcessor的方式,这就是<context:annotation-config/>。请看下面的配置:


清单 19. 调整 beans.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
   <context:annotation-config/>

   <bean id="boss"class="com.baobaotao.Boss"/>
   <bean id="office"class="com.baobaotao.Office">
       <property name="officeNo"value="001"/>
   </bean>
   <bean id="car" class="com.baobaotao.Car"scope="singleton">
       <property name="brand" value=" 红旗CA72"/>
       <property name="price"value="2000"/>
   </bean>
</beans>


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

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


转:<context:annotation-config/>
转:<context:annotation-config/>

使用 @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

package com.baobaotao;

import org.springframework.stereotype.Component;

@Componentpublic class Car { …}


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


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

package com.baobaotao;

import org.springframework.stereotype.Component;

@Componentpublic class Office {

private String officeNo = "001";

}


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


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

package com.baobaotao;

importorg.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
importorg.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("boss")
public class Boss {
   @Autowired
    private Carcar;

   @Autowired
    privateOffice office;
   
}


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

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


清单 23. 简化版的 beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">
   <context:component-scanbase-package="com.baobaotao"/>
</beans>

这 里,所有通过 <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+

下面是一个简单的例子:

<context:component-scanbase-package="com.baobaotao">
   <context:include-filter type="regex"
       expression="com\.baobaotao\.service\..*"/>
   <context:exclude-filter type="aspectj"
       expression="com.baobaotao.util..*"/>
</context:component-scan>

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

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


清单 24. 通过 @Scope 指定 Bean 的作用范围
package com.baobaotao;
import org.springframework.context.annotation.Scope;

@Scope("prototype")
@Component("boss")
public class Boss {
   
}

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


转:<context:annotation-config/>
转:<context:annotation-config/>

采用具有特殊语义的注释

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


转:<context:annotation-config/>
转:<context:annotation-config/>

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

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

注 释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。 如果 Bean不是自己编写的类(如JdbcTemplate、SessionFactoryBean等),注释配置将无法实施,此时 XML配置是唯一可用的方式。 注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于@Transaction事务注释,使用aop/tx 命名空间的事务配置更加灵活和简单。

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


转:<context:annotation-config/>
转:<context:annotation-config/>

小结

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



参考资料

学习
Spring系列:Spring 框架简介:优秀的 Spring 框架入门系列,了解 Spring 框架的基本概念。

轻量级开发的成功秘诀,第3 部分: Spring 露出水面:介绍了在 Spring 框架的轻量级 Ioc 容器。SpringFramework 和 IBM WebSphere Application Server:Interface21 的首席执行官Rod Johnson 和 IBM 的 WebSphere Open Source 主管 Paul Buck 讨论了 SpringFramework 通过 IBM WebSphere Application Server 认证对 Spring 和WebSphere 产品系列的开发人员和客户有何重要意义。

Tiger中的注释,第 1 部分: 向 Java 代码中添加元数据:解释了元数据如此有用的原因,向您介绍了 Java语言中的注释,并研究了 Tiger 的内置注释。

Tiger中的注释,第 2 部分: 定制注释:说明了如何创建定制注释,如何用自己的注释注解文档,并进一步定制代码。
0 0