如何将自动装配的spring bean转储到xml配置文件?

时间:2013-11-07 08:02:18

标签: xml spring spring-mvc annotations

我正在使用带有spring-mvc和spring-security的@Autowired注释,它可以工作,但是webapp开始非常慢,每次大约1分钟,因为spring-mvc和spring-security扫描两次所有自动安装的类和班级总人数约500人。 有什么建议可以加快扫描时间吗?或静态xml配置更好?

web.xml中的

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/rest-servlet.xml
    </param-value>
</context-param>
    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

    <servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>
    <servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
    ....

在rest-sevlet.xml中

    <context:component-scan base-package="com.mycomp" />
<mvc:annotation-driven />
<mvc:interceptors>
    ....
    </mvc:interceptors>
<import resource="classes/config/applicationContext-security-base.xml"/>
<import resource="classes/config/applicationContext-security.xml"/>

<import resource="classes/config/spring-aop.xml"/>
<!-- i18n --> 
<import resource="classes/config/spring-locale.xml"/>

1 个答案:

答案 0 :(得分:2)

首先不要加载您的配置两次。目前,ContextLoaderListenerDispatcherServlet都加载了相同的配置文件。结果重复bean实例,重复扫描(以及未来内存问题旁边的那些,奇怪的事务问题等)。

您的配置需要拆分。您的ContextLoaderListener仅加载一般的内容到您的应用程序(服务,存储库,数据源等)。反过来,DispatcherServlet应该包含/加载仅与Web相关的内容,例如(@)ControllersViewResolversView s,mvc配置等。

还要注意组件扫描,不要陷入每个人的陷阱。如果将相同组件扫描添加到每个配置文件,这将导致bean重复(永远bean被实例化两次)。因此,请确保您的ContextLoaderListener' scans for everything but controllers and that your DispatcherServlet仅扫描与Web相关的内容(例如控制器)。

相关问题