之前用springmvc的时候,会将mvc的容器和spring根容器分开配置,同时指定根容器扫描的目标和mvc容器扫描的目标,往往是这样子:
<!-- 跟容器排除@Controller注解 -->
<context:component-scan base-package="org.xxx" use-default-filter="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- mvc容器只包含@Controller注解 -->
<context:component-scan base-package="org.xxx" use-default-filter="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
OK,完全可以,而且大多数都是这么省事儿的一写就行了。
但是今天我很搓火,因为启动一个项目太慢了,一直在等spring上下文初始化完成,而且根和mvc上下文初始化时间差不多。
于是我就打开spring的debug级别的log,我了个擦,无论是根还是mvc容器都把整个项目的class扫描了一边,一边扫描一边利用上面的“包含/排除”过滤器验证。
---------------------------
之后我一琢磨,觉得直接在 base-package 上定义好 根上下文和mvc上下文的 起点目录应该会更好一些,这样的话每个上下文只需先验证目录(包)名是否符合然后再扫描下面的class岂不是会省去大量的扫描时间。
于是我先搜索了一下base-package 通配模式,果然支持ant样式,*和**
重新配置了以下自动扫描:
<!-- 根上下文只扫描 dao, service.impl 以及 webservice 包下的class -->
<context:component-scan base-package="org.xxx.**.dao, org.xxx.**.service.impl, org.xx.**.webservice" />
<!-- mvc上下文只扫描 controller 包下的class -->
<context:component-scan base-package="org.xxx.**.controller" />
直接使用默认的过滤器就行了,貌似根本不用在去定义包含或排除的过滤器,而且初始化速度杠杠的 , 用debug-log查看了以下spring的输出,完全没问题,也没有出现两容器重复bean的情况。
--------------------------------------