context:include-filter与context:exclude-filter的区别

本文介绍如何使用Spring框架的<context:component-scan>标签来扫描指定的包及其子包中的注解,包括通过<include-filter>和<exclude-filter>来精确控制扫描范围的方法。特别讲解了如何针对@Controller注解进行包含或排除的扫描。

本文为本人从各处整理出来的资料,侵删!

<!-- 扫描@Controller注解 -->
<context:component-scan base-package="com.fq.controller">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 配置扫描注解,不扫描@Controller注解 -->
<context:component-scan base-package="com.fq">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>


<context:include-filter> 可以搜索@Controller标签
<context:exclude-filter> 搜索不到@Controller标签
--------------------------------------------------------------------

注意:1. 不得不提醒下. 使用context:include-filter时,一定要在context:component-scan ;中设置属性use-default-filters=false; 否则还是用的默认的filter;而使用context:exclude-filter时就没这个必要;

           2.最好的解释就是: context:exclude-filter是设置黑名单。比如手机黑名单,在黑名单中的人是无法打进电话的。 context:include-filter是设置白名单。在手机白名单中的号码可以打进来,不在白名单也不在黑名单中的也可以打进来。 所以需要针对白名单过滤进行设置,把默认的过滤器关闭,即use-default-filters=&amp;quot;true&amp;quot;。然后他就仅仅扫描指定注解了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值