java.lang.IllegalStateException:无法初始化上下文,因为已经存在根应用程序上下文 [英] java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present
问题描述
运行我的Web应用程序时,每隔一次尝试,我都会得到下面列出的stacktrace.请注意,据我所知,在web.xml中似乎没有多个ContextLoader定义.此外,该应用程序可以在第二/第四/等位置正常运行.时间.这种行为比根本行不通的调试起来要困难得多.谁能对此有所启发?
When running my webapp, I get the stacktrace listed below every other try. Note that there doesn't seem to be multiple ContextLoader definitions im web.xml as far as I can tell. Moreover, the app runs just fine the second/fourth/etc. time. This behaviour is much harder to debug than if it simply didn't work. Can anyone shed some light on this?
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)
推荐答案
对于有类似问题的任何人-事实证明,该项目中使用的spring-jersey正在建立自己的上下文.我的上下文和春季球衣显然是按随机顺序初始化的.
此处更多信息:
https://java.net/jira/browse/JERSEY-2038
https://java.net/projects/jersey/lists/users/archive/2014-03/message/124
建议的解决方案:
For anyone with a similar problem - turns out that spring-jersey used in the project was setting up its own context. My context and the spring-jersey one were initialized in random order apparently.
More info here:
https://java.net/jira/browse/JERSEY-2038
https://java.net/projects/jersey/lists/users/archive/2014-03/message/124
The suggested solution of adding:
servletContext.setInitParameter("contextConfigLocation", "<NONE>");
由于初始化顺序,在WebAppInitializer中,实现无法可靠地工作.解决该问题的方法是添加它的xml等效项:
In WebAppInitializer implementation didn't work reliably due to initialization order. What solved the problem was adding its xml equivalent:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</context-param>
作为web.xml中的firt参数,请确保在初始化上下文之前将其设置.
as the firt parameter in web.xml, ensuring that its set before the context is initialized.
这篇关于java.lang.IllegalStateException:无法初始化上下文,因为已经存在根应用程序上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!