spring component-scan filter

这个文件中beans根节点下只有一个context:component-scan节点,此节点有两个属性base-package属性告诉spring要扫描的包,use-default-filters="false"表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Repository,Controller注解修饰的类,而此处我们处于示例的目的,故意将use-default-filters属性设置成了false。

context:component-scan节点允许有两个子节点<context:include-filter>和<context:exclude-filter>。filter标签的type和表达式说明如下:

Filter Type Examples Expression Description include-filter为例
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class

<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>

表示扫描base-package下的类上加了Aspect注解的类,并注册到spring的bean容器

assignable org.example.SomeClass 指定class或interface的全名

<context:include-filter type="assignable" expression="com.test.scan.StuService"/>

指定扫描StuService类作为bean

aspectj org.example..*Service+ AspectJ語法  
regex org\.example\.Default.* Regelar Expression  
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter  

 

 

 

 

 

 

 

 

 

 

在我们的示例中,将filter的type设置成了正则表达式,regex,注意在正则里面.表示所有字符,而\.才表示真正的.字符。我们的正则表示以Dao或者Service结束的类。

我们也可以使用annotaion来限定,如下:

<?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.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="cn.outofmemory.spring" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>  <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>  </context:component-scan> </beans>

 这里我们指定的include-filter的type是annotation,expression则是注解类的全名。

另外context:conponent-scan节点还有<context:exclude-filter>可以用来指定要排除的类,其用法和include-filter一致。

 

<转:http://outofmemory.cn/java/spring/spring-DI-with-annotation-context-component-scan>

 

use-default-filters默认值为true,也就是说sping默认会扫描Service,Component,Repository,Controller注解的bean。

其他加的include-filter和exclude-filter都是在以上的基础上增加的扫描规则。

public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
    {
        resourcePatternResolver = new PathMatchingResourcePatternResolver();
        metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
        resourcePattern = "**/*.class";
        if(useDefaultFilters)
            registerDefaultFilters();
    }
 protected void registerDefaultFilters()
    {
        includeFilters.add(new AnnotationTypeFilter(org/springframework/stereotype/Component));
        ClassLoader cl = org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.getClassLoader();
        try
        {
            includeFilters.add(new AnnotationTypeFilter(cl.loadClass("javax.annotation.ManagedBean"), false));
            logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
        }
        catch(ClassNotFoundException _ex) { }
        try
        {
            includeFilters.add(new AnnotationTypeFilter(cl.loadClass("javax.inject.Named"), false));
            logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
        }
        catch(ClassNotFoundException _ex) { }
    }

 

excludeFilters优先于includeFilters
protected boolean isCandidateComponent(MetadataReader metadataReader)
        throws IOException
    {
        for(Iterator iterator = excludeFilters.iterator(); iterator.hasNext();)
        {
            TypeFilter tf = (TypeFilter)iterator.next();
            if(tf.match(metadataReader, metadataReaderFactory))
                return false;
        }

        for(Iterator iterator1 = includeFilters.iterator(); iterator1.hasNext();)
        {
            TypeFilter tf = (TypeFilter)iterator1.next();
            if(tf.match(metadataReader, metadataReaderFactory))
                return true;
        }

        return false;
    }

 

你可能感兴趣的:(spring component-scan filter)