首页 > 解决方案 > 多次创建 ApplicationContext(在 struts + Spring 项目中)

问题描述

我有一个遗留系统,在 struts 1 和 spring 3 中,现在我需要将 spring 3 的版本升级到 5。我已经完成了几乎所有的事情,并且一切正常。当我启动tomcat服务器时,我只面临一个问题。

让我们看看代码:- 这是之前的 web.xml 配置,

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>
            org.springframework.web.context.ContextLoaderServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet> 

升级版本后:-

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

  <servlet>
        <servlet-name>servlet</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
          <param-name>config</param-name>
          <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>       
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>       
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>  

    <servlet-mapping>
        <servlet-name>servlet</servlet-name>
        <url-pattern>*.do</url-pattern>
   </servlet-mapping>

现在,当我启动我的 tomcat 服务器时,会创建两次 applicationContext 环境。一个用于“org.apache.struts.action.ActionServlet”,第二个用于“org.springframework.web.context.ContextLoaderListener”

检查日志:

INFO: TLD skipped. URI: http://struts.apache.org/tags-nested is already defined
May 01, 2020 11:14:09 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
May 01, 2020 11:14:09 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
May 01, 2020 11:14:09 AM org.apache.catalina.core.ApplicationContext log

// First time
INFO: Initializing Spring root WebApplicationContext  


log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
Calling the CacheManager Constructor...
May 01, 2020 11:14:17 AM org.apache.catalina.core.ApplicationContext log
// Second time
INFO: Initializing WebApplicationContext for Struts ActionServlet 'servlet', module ''  

May 01, 2020 11:14:19 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-9090"]
May 01, 2020 11:14:19 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-9009"]
May 01, 2020 11:14:19 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 18889 ms

建议我如何解决这个问题。

标签: javaspringtomcatwebstruts

解决方案


推荐阅读