Spring(16)——使用注解进行bean定义(二)

16.5 过滤器

默认情况下,Spring将扫描所有@Component、@Controller、@Service和@Repository标注的类,以及这几个注解所派生出来的注解所标注的类,如之前介绍的自定义的@MyComponent注解,并将它们作为一个bean定义在对应的bean容器中。Spring提供了两个filter定义可以让我们对需要扫描的类进行进一步的过滤,一个是<context:include-filter/>,一个是<context:exclude-filter/>。这两个filter的配置基本是一样的,所不同的是它们的功能。<context:include-filter/>对需要扫描的内容进行过滤,即告诉Spring除了按照默认的逻辑进行扫描以外,还需要对哪些内容进行扫描。而<context:exclude-filter/>则是用来对不需要进行扫描的内容进行过滤,即告诉Spring应该排除对哪些原本应该扫描的内容进行扫描。它们都是作为<context:component-scan/>的子元素进行定义的,我们可以在一个<context:component-scan/>标签下单独同时定义多个<context:include-filter/>或者是多个<context:exclude-filter/>,但是如果在一个<context:component-scan/>标签下同时定义<context:include-filter/><context:exclude-filter/>则需要先定义<context:include-filter/>,再定义<context:exclude-filter/>

在定义<context:include-filter/><context:exclude-filter/>时我们必须指定type属性和expression属性。type属性表示按照哪种类型进行过滤,expression则表示需要进行过滤的表达式。

对于type属性而言,我们可选的值有如下五种类型。

  • annotation:表示注解,根据注解来扫描目标类,即目标类应该使用哪个注解进行了标注,对应的expression应该是注解的全名称。
  • assignable:表示是从哪个类或接口派生的,即目标类继承了哪个类或实现了哪个接口,对应的expression应该是对应接口或类的全名称。
  • aspect:表示将使用Aspect类型的表达式来表示目标类。
  • regex:表示将使用正则表达式来匹配目标类名称。
  • custom:表示将使用自定义的org.springframework.core.type.TypeFilter来匹配对应的类。

16.5.1 annotation

如下示例表示我们将排除对使用了@Service注解进行标注的类的扫描,即不将对应的Class加入bean容器中。

<context:component-scan base-package="com.app">	
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

这里再重申一点,那就是使用Spring在扫描时默认是会扫描base-package指定的所有包及其子孙包下所有使用@Component、@Controller、@Service和@Repository及其其它使用这些注解进行标注的注解,使用这些注解进行标注过的Class都会被加入bean容器中。通过<context:include-filter/>我们可以将其它不在这些范畴的Class加入到bean容器中,通过<context:exclude-filter/>我们可以将在这个范畴的Class不纳入到加入bean容器中的范畴。

所以我们也可以通过<context:include-filter/>来将对使用了某种类型的注解进行标注的Class加入bean容器中。如下示例,我们将之前介绍的@MyComponent定义成一个普通的注解,然后将其纳入到bean容器的范畴即可让Spring将使用@MyComponent进行标注的Class加入到bean容器中。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyComponent {

	public String value() default "";
	
}
<context:component-scan base-package="com.app">	
	<context:include-filter type="annotation" expression="com.app.MyComponent"/>
</context:component-scan>

16.5.2 assignable

assignable表示根据继承的类或实现的接口进行扫描。如下表示将扫描所有继承或实现java.lang.Object的类,并将其加入bean容器,通过这种方式我们可以让Spring将某些包下的所有的类都加入Spring的bean容器。

<context:component-scan base-package="com.app">	
	<context:include-filter type="assignable" expression="java.lang.Object"/>
</context:component-scan>

16.5.3 aspect

当指定type为aspect时表示将以Spring定义切面的形式来指定对应的需要进行匹配的Class。如下示例则表示匹配com.app包及其子孙包下所有名称以Bean开头的Class,并将它们加入bean容器。使用该type时需要将spring-aspects-xxx.jar加入类路径下。

<context:component-scan base-package="com.app">	
	<context:include-filter type="aspectj" expression="com.app..Bean*"/>
</context:component-scan>

16.5.4 regex

regex表示将采用正则表达式进行匹配。如下示例就表示通过正则表达式匹配所有的Class的名称以Bean开始的类,并将它们加入bean容器中(点“.”在正则表达式中代表任意字符)。

<context:component-scan base-package="com.app">	
	<context:include-filter type="regex" expression=".*\.Bean.*"/>
</context:component-scan>

16.5.5 custom

custom表示使用自己定义的TypeFilter进行匹配。下面来看一个示例,我们来定义一个自己的TypeFilter,其实现简单的根据Class的名称进行匹配,只要Class的名称中包含Bean即表示匹配成功。

public class MyTypeFilter implements TypeFilter {

	public boolean match(MetadataReader metadataReader,
			MetadataReaderFactory metadataReaderFactory) throws IOException {
		String className = metadataReader.getClassMetadata().getClassName();
		return className.contains("Bean");
	}

}

之后我们在定义filter时就可以通过type指定为custom,然后对应的expression属性指定我们的自定义TypeFilter即可。

<context:component-scan base-package="com.app">	
	<context:include-filter type="custom" expression="com.app.MyTypeFilter"/>
</context:component-scan>

(注:本文是基于Spring4.1.0所写)

猜你喜欢

转载自elim.iteye.com/blog/2388634