码迷,mamicode.com
首页 > 其他好文 > 详细

context:component-scan扫描使用的use-default-filters

时间:2016-07-28 15:01:21      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

  如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean。

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

  但是如下方式,不仅仅扫描@Controller,还扫描@Service/@Repository的Bean,可能造成一些问题

  此处只应该加载表现层组件,如果此处还加载dao层或service层的bean会将之前容器加载的替换掉,而且此处不会进行AOP织入,所以会造成AOP失效问题(如事务不起作用)。

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

  注意:xx.xx.xx包下可能包括controller、service等。

  <context:component-scan>的use-default-filters属性,则默认为true。ClassPathBeanDefinitionScanner会自动注册对@Component、@ManagedBean、@Named注解的Bean进行扫描。

  在进行扫描时会通过include-filter/exclude-filter来判断你的Bean类是否是合法的:即首先通过exclude-filter 进行黑名单过滤;然后通过include-filter 进行白名单过滤;否则默认排除。

  所以不需要扫描@Service/@Repository的Bean,则use-default-filters=“false”禁用掉。

   参考博客:http://jinnianshilongnian.iteye.com/blog/1423971

context:component-scan扫描使用的use-default-filters

标签:

原文地址:http://www.cnblogs.com/lcngu/p/5714673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!