include-filter和exclude-filter

context命名空间的component-scan通过扫描base-package属性指定的类包,从类的注解信息中获取bean的定义信息

<context:component-scan base-package="com.xxx" use-default-filters="true"/>

并通过<context:include-filter>和<context:exclude-filter>对信息进行过滤:

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

其中,use-default-filters是一个不容忽视的属性,默认值为true,表示默认会对@Component、@Controller、@Service、@Reposity标注的bean进行扫描。<context:component-scan>先根据<context:exclude-filter>列出需要排除的黑名单,再通过<context:include-filter>列出需要包含的白名单。

下面的反例中,不但会扫描@Controller的bean,还会扫描@Component、@Service、@Reposity的bean

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

原因如上所述,use-default-filters默认为true,所以要显示指定为false

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

总结:属性use-default-filters="false"和<context:include-filter>要一起使用,表示:只扫描指定注解的类。

            <context:exclude-filter>直接使用,表示:不扫描指定注解的类,其他注解类全部扫描。 

  • 11
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值