context:component-scan扫描使用的use-default-filters
如下方式可以成功掃描到@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,可能造成一些問題
此處只應(yīng)該加載表現(xiàn)層組件,如果此處還加載dao層或service層的bean會將之前容器加載的替換掉,而且此處不會進(jìn)行AOP織入,所以會造成AOP失效問題(如事務(wù)不起作用)。
<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屬性,則默認(rèn)為true。ClassPathBeanDefinitionScanner會自動注冊對@Component、@ManagedBean、@Named注解的Bean進(jìn)行掃描。
在進(jìn)行掃描時(shí)會通過include-filter/exclude-filter來判斷你的Bean類是否是合法的:即首先通過exclude-filter 進(jìn)行黑名單過濾;然后通過include-filter?進(jìn)行白名單過濾;否則默認(rèn)排除。
所以不需要掃描@Service/@Repository的Bean,則use-default-filters=“false”禁用掉。
? 參考博客:http://jinnianshilongnian.iteye.com/blog/1423971
轉(zhuǎn)載于:https://www.cnblogs.com/lcngu/p/5714673.html
總結(jié)
以上是生活随笔為你收集整理的context:component-scan扫描使用的use-default-filters的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机专业运动会口号,运动会口号押韵有气
- 下一篇: 大数据的大价值:大数据五大成功案例深度解