spring中的context:include-filter和context:exclude-filter的区别

(1)在Spring-MVC.xml中有以下配置:

 

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

可以看出要把最终的包写上,而不能这样写base-package=”com.fq”。这种写法对于include-filter来讲它都会扫描,而不是仅仅扫描@Controller。哈哈哈,这点需要注意。他一般会导致一个常见的错误,那就是事务不起作用,补救的方法是添加use-default-filters=”false”。

(2)在Spring-common.xml中有如下配置:

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

可以看到,他是要扫描com.fq包下的所有子类,不包含@Controller。对于exculude-filter不存在包不精确后都进行扫描的问题。


1 在主容器中(applicationContext.xml),将Controller的注解打消掉

[html]  view plain  copy
  1. <context:component-scan base-package="exampleBean">  
  2.         <context:exclude-filter type="annotation"  
  3.             expression="org.springframework.stereotype.Controller" />  
  4.     </context:component-scan>  

2 在主容器中(applicationContext.xml),按正则过滤指定类

[html]  view plain  copy
  1. <context:component-scan base-package="exampleBean">  
  2. <!-- 过滤掉exampleBean下的且类名以Default开头的类 -->  
  3.     <context:exclude-filter type="regex"  
  4.         expression="exampleBean\.Default*" />  
  5. </context:component-scan>  

3 在主容器中(applicationContext.xml),按指定类名过滤指定类

[html]  view plain  copy
  1. <context:component-scan base-package="exampleBean">  
  2. <!-- 过滤掉exampleBean下的类名为<span style="font-family: Arial, Helvetica, sans-serif;">MovieFinder</span>的类 -->  
  3.     <context:exclude-filter type="assignable" expression="exampleBean.MovieFinder"/>  
  4. </context:component-scan>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值