• Spring框架参考手册(4.2.6版本)翻译——第三部分 核心技术 6.10.4 使用过滤器自定义扫描


    6.10.4 使用过滤器自定义扫描

    默认情况下,使用@Component@ Repository@ Service@ Controller自身使用@Component解标记的自定义注解标记的类是唯一检测到的候选组件。然而,您可以通过应用自定义筛选器来修改和扩展此行为。将它们添加为@ComponentScanincludeFiltersexcludeFilters参数(或者作为component-scan元素的include-filterexclude-filter子元素)。每个filter元素都要有typeexpression属性。下表介绍了筛选选项。

    6.5. 过滤器类型

    过滤器类型

    表达式例子

    描述

    annotation(默认)

    org.example.SomeAnnotation

    在目标组件中以类型级别呈现注解

    assignable

    org.example.SomeClass

    目标组件指定(扩展/实现)的类(或接口)

    aspectj

    org.example..*Service+

    由目标组件匹配的AspectJ类型表达式

    regex

    org.example.Default.*

    由目标组件类名匹配的正则表达式

    custom

    org.example.MyTypeFilter

    org.springframework.core.type .TypeFilter接口的自定义实现

    以下示例展现了忽略所有@Repository解而使用“stub”存储库的配置。

    @Configuration
    @ComponentScan(basePackages = "org.example",
            includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
            excludeFilters = @Filter(Repository.class))
    public class AppConfig {
        ...
    }

    跟使用XML是等效的

    <beans>
        <context:component-scan base-package="org.example">
            <context:include-filter type="regex"
                    expression=".*Stub.*Repository"/>
            <context:exclude-filter type="annotation"
                    expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
    </beans>

    您还可以通过在注上设置useDefaultFilters = false或提供use-default-filters =“false”作为<component-scan />元素的属性来禁用默认过滤器。 这实际上将禁止自动检测使用@ Component@ Repository@ Service@Controller的类。

  • 相关阅读:
    facesconfig详解 Crazy
    java反射学习之一反射机制概述
    axis2开发webservice程序
    java反射学习之二万能EXCEL导出
    JMF框架
    解决FF浏览器无法执行window.close()脚本
    ava:String和Date、Timestamp之间的转换
    java获取当前路径。
    JSP 动态验证码。
    dojo.mixin
  • 原文地址:https://www.cnblogs.com/springmorning/p/10446269.html
Copyright © 2020-2023  润新知