srping mvc 集成CXF 导致类初始化两遍

cxf依赖于spring的ContextLoaderListener,而spring mvc 则依赖于DispatcherServlet。

初始化DispatcherServlet的时候会依赖初始化一个spring 容器,此容器为spring全局容器WebApplicationContext的子容器,如果调用相同的配置文件初始化将导致所有类初始化两次,造成不确定的错误。

spring的字容器可以访问父容器内的对象,既然如此,可以让全局容器加载除Controller层以外的类,而让spring mvc容器加载Controller层的类,来解除此问题。

具体配置实现:

全局spring包扫描配置:

<context:component-scan base-package="com.blackbread">
<!-- 不加载org.springframework.stereotype.Controller类型注解 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

spring mvc 包扫描配置:

<context:component-scan base-package="com.blackbread"
use-default-filters="false">
<!-- 只载org.springframework.stereotype.Controller类型注解 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

在web.xml中分别加载不同的配置文件,即可解决所有类加载两遍问题。

上一篇:利用 SHELL 的 ITaskbarList 接口控制 TaskBar 图标


下一篇:Linux df du 命令