http://jinnianshilongnian.iteye.com/blog/1762632html
http://blog.51cto.com/wenshengzhu/1700340spring
http://www.cnblogs.com/kevin-yuan/p/5068448.htmlexpress
【转载】http://www.javashuo.com/article/p-pridpmyv-dh.htmlspa
在Spring MVC中的配置中通常会遇到这两个标签,做为<context:component-scan>的子标签出现。code
存疑:component
context:include-filter对于include, 除了扫描base-package包下面的子类,还扫描expression后面的包。
context:exclude-filter对于exclude,即便expression后面的包在base-package下面,也不扫描。
但在使用时要注意一下几点:xml
1.在不少配置中通常都会吧Spring-common.xml和Spring-MVC.xml进行分开配置,这种配置就行各施其职同样,显得特别清晰。htm
在Spring-MVC.xml中只对@Controller进行扫描就可,做为一个控制器,其余的事情不作。blog
在Spring-common.xml中只对一些事务逻辑的注解扫描。事务
2.如今给定一个项目包的机构:
com.fq.controlller
com.fq.service
就先给定这两个包机构
(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不存在包不精确后都进行扫描的问题。
Spring过滤不须要扫描的包
<context:component-scan base-package="com.alimama"> <!-- 不扫描com.alimama.trace包--> <context:exclude-filter type="regex" expression="com.alimama.trace.*"/> </context:component-scan>