java.lang.IllegalStateException:无法初始化上下文,因为已存在根应用程序上下文

时间:2015-11-10 15:06:07

标签: spring illegalstateexception applicationcontext

运行我的webapp时,每隔一次尝试就会看到下面列出的堆栈跟踪。请注意,据我所知,似乎不是web.xml中的多个ContextLoader定义。此外,应用程序运行良好的第二/第四/等。时间。这种行为比它根本不起作用更难调试。任何人都可以对此有所了解吗?

 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:299)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4795)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5221)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:919)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1703)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:6)

对于任何有类似问题的人 - 结果表明项目中使用的弹力球衣正在建立自己的背景。我的背景和春季运动衫显然是以随机顺序初始化的。 更多信息:
https://java.net/jira/browse/JERSEY-2038
https://java.net/projects/jersey/lists/users/archive/2014-03/message/124
建议的添加解决方案:

servletContext.setInitParameter("contextConfigLocation", "<NONE>");

在WebAppInitializer中,由于初始化顺序,实现无法可靠地工作。解决问题的是添加xml等价物:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value></param-value>
</context-param> 

作为web.xml中的firt参数,确保在初始化上下文之前设置它。

答案 1 :(得分:1)

如果您使用弹簧靴

,请确保您没有这样做
  container.addListener(new ContextLoaderListener(rootContext));
相关问题