zoukankan      html  css  js  c++  java
  • 关于context:component-scan配置中use-default-filters参数的作用

    参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下:

    <context:component-scan base-package="com.jaamy"  use-default-filters="false"><context:include-filter type="annotation"  expression="org.springframework.stereotype.Controller" /></context:component-scan>

     这个只扫描com.jaamy包下的@Controller,不会扫描@Service、@Repository

     

    <context:component-scan base-package="com.jaamy"><context:include-filter type="annotation"  expression="org.springframework.stereotype.Controller" /></context:component-scan>

     这个不但扫描com.jaamy包下的@Controller,同时也会扫描@Service、@Repository,注意这里没有添加use-default-filters参数

     

    下面配合源码说明下use-default-filters参数的作用以及和context:include-filter、exclude-filter的关系。

    代码中是根据use-default-filters的值来确定是否需要调用registerDefaultFilters来添加默认的filters到includeFilters中,看下面的代码:

    org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

    复制代码
    public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) {
            if (useDefaultFilters) {
                registerDefaultFilters();
            }
            Assert.notNull(environment, "Environment must not be null");
            this.environment = environment;
        }
    复制代码

     

    use-default-filters为true时调用了下面的代码,在includeFilters列表中添加了Component、ManagedBean和Named,因此use-default-filters的值直接影响includeFilters的内容,而includeFilters的容直接影响了要扫描的内容,因此use-default-filters的值是否配置也就决定了整体要扫描的内容。

    复制代码
    protected void registerDefaultFilters() {
            this.includeFilters.add(new AnnotationTypeFilter(Component.class));
            ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
            try {
                this.includeFilters.add(new AnnotationTypeFilter(
                        ((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
                logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
            }
            catch (ClassNotFoundException ex) {
                // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.        }
            try {
                this.includeFilters.add(new AnnotationTypeFilter(
                        ((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
                logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
            }
            catch (ClassNotFoundException ex) {
                // JSR-330 API not available - simply skip.        }
        }
    复制代码

     

    关于include-filter、exclude-filter的作用主要是用来过滤扫描到的bean是否合法:

    首先通过exclude-filter进行黑名单过滤;

    然后通过include-filter进行白名单过滤;

    否则默认排除。 看下面的代码:

    复制代码
    protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
            for (TypeFilter tf : this.excludeFilters) {
                if (tf.match(metadataReader, this.metadataReaderFactory)) {
                    return false;
                }
            }
            for (TypeFilter tf : this.includeFilters) {
                if (tf.match(metadataReader, this.metadataReaderFactory)) {
                    return isConditionMatch(metadataReader);
                }
            }
            return false;
        }
    复制代码

     

    总结:

    <context:component-scan base-package="com.jaamy"  use-default-filters="false">
             <context:include-filter type="annotation"  expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    针对上面的配置,use-default-filters的作用如下:

    use-default-filters不配置或者是配置为true时:

    不但要扫描include配置的com.jaamy包下的@Controller,而且还要扫描@Service和@Repository

    use-default-filters配置为false时:

    只扫描include配置的com.jaamy包下的@Controller,不扫描@Service和@Repository

     






  • 相关阅读:
    Codeforces Round #392 (Div. 2)
    hihocoder #1419 : 后缀数组四·重复旋律4
    hihocoder #1415 : 后缀数组三·重复旋律3
    LOJ #6284. 数列分块入门 8-分块(区间查询等于一个数c的元素,并将这个区间的所有元素改为c)
    LOJ #6283. 数列分块入门 7-分块(区间乘法、区间加法、单点查询)
    LOJ #6282. 数列分块入门 6-分块(单点插入、单点查询、数据随机生成)
    LOJ #6281. 数列分块入门 5-分块(区间开方、区间求和)
    LOJ #6280. 数列分块入门 4-分块(区间加法、区间求和)
    LOJ #6279. 数列分块入门 3-分块(区间加法、查询区间内小于某个值x的前驱(比其小的最大元素))
    LOJ #6278. 数列分块入门 2-分块(区间加法、查询区间内小于某个值x的元素个数)
  • 原文地址:https://www.cnblogs.com/jeffen/p/6397698.html
Copyright © 2011-2022 走看看