Spring 配置项之 <context:component-scan base-package="..."/>

使用 @Component

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

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

使用 @Component 注释的 Car.javapackage com.ossez; import org.springframework.stereotype.Component; @Componentpublic class Car { …} 仅需要在类定义处,使用@Component注释就可以将一个类定义了 Spring 容器中的 Bean。下面的代码将Office定义为一个 Bean:

使用 @Component 注释的 Office.javapackage com.ossez; import org.springframework.stereotype.Component; @Componentpublic class Office { private String officeNo = "001"; … } 这样,我们就可以在 Boss 类中通过@Autowired注入前面定义的Car和Office Bean了。

使用 @Component 注释的 Boss.java[code]package com.ossez;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component(“boss”)
public class Boss {
@Autowired
private Car car;

@Autowired   
private Office office;   
…   

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

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

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

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

扫描过滤方式

注释假如 com.ossez.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。类名指定通过全限定类名进行过滤,如您可以指定将 com.ossez.Boss 纳入扫描,而将 com.ossez.Car 排除在外。正则表达式通过正则表达式定义过滤的类,如下所示: com.ossez.Default.*AspectJ 表达式通过 AspectJ 表达式定义过滤的类,如下所示: com. ossez…*Service+

下面是一个简单的例子:<context:component-scan base-package="com.ossez"> <context:include-filter type="regex" expression="com\.ossez\.service\..*"/> <context:exclude-filter type="aspectj" expression="com.ossez.util..*"/> </context:component-scan> 值得注意的是 context:component-scan/ 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用 context:component-scan/ 后,就可以将 context:annotation-config/ 移除了。

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

通过 @Scope 指定 Bean 的作用范围package com.ossez; import org.springframework.context.annotation.Scope; … @Scope("prototype") @Component("boss") public class Boss { … }