Spring启动 - web.xml中的多个ContextLoader *定义

时间:2017-09-25 09:10:27

标签: spring spring-mvc spring-boot

我正在尝试将现有的Spring Web应用程序( Spring版本4.1.X )迁移到Spring启动(版本1.3 ),

我在应用程序中使用web.xml,我已经配置了监听器。

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.package.config.WebApplicationContextConfig</param-value>
</context-param>

<listener>
    <listener-class>com.package.application.ApplicationContextLoaderListener</listener-class>
</listener>

<servlet>
    <description>
    </description>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>com.package.servlet.ApplicationServlet</servlet-class>
    <init-param>
        <description>contextClass</description>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.package.config.SpringWebMvcConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>      

在监听器中,我调用super contextIntialized。

public class ApplicationContextLoaderListener extends ContextLoaderListener

public void contextInitialized(ServletContextEvent event) {
    super.contextInitialized(event);

应用程序在ContextLoader.class

中可用的以下位置失败
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
    if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
        throw new IllegalStateException(
                "Cannot initialize context because there is already a root application context present - " +
                "check whether you have multiple ContextLoader* definitions in your web.xml!");
    }

我遇到错误。

java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:297) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) ~[spring-web-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at com.package.application.ApplicationContextLoaderListener.contextInitialized(ApplicationContextLoaderListener.java:47) ~[classes/:1.0-SNAPSHOT]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842) [catalina.jar:8.0.36]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303) [catalina.jar:8.0.36]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [catalina.jar:8.0.36]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407) [catalina.jar:8.0.36]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397) [catalina.jar:8.0.36]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_31]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_31][enter image description here][1]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_31]

应用程序中使用的罐子放在下面。 enter image description here

0 个答案:

没有答案