Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring java.lang.IllegalStateException:无法初始化上下文,因为已经存在根应用程序上下文_Spring_Illegalstateexception_Applicationcontext - Fatal编程技术网

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

Spring java.lang.IllegalStateException:无法初始化上下文,因为已经存在根应用程序上下文,spring,illegalstateexception,applicationcontext,Spring,Illegalstateexception,Applicationcontext,当运行我的webapp时,我每隔一次就会得到下面列出的stacktrace。注意,据我所知,im web.xml中似乎没有多个ContextLoader定义。此外,该应用程序在第二次/第四次等时间运行良好。这种行为要比根本不起作用更难调试。有人能解释一下吗 java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present -

当运行我的webapp时,我每隔一次就会得到下面列出的stacktrace。注意,据我所知,im 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)

对于任何有类似问题的人来说,项目中使用的SpringJersey都是在建立自己的环境。显然,我的上下文和spring jersey的上下文是按随机顺序初始化的。 更多信息请点击此处:


建议的解决办法是添加:

servletContext.setInitParameter("contextConfigLocation", "<NONE>");
servletContext.setInitParameter(“contextConfigLocation”,即“”);
在WebAppInitializer中,由于初始化顺序,实现无法可靠地工作。解决这个问题的方法是添加它的xml等价物:

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

上下文配置位置

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

如果使用spring boot,请确保没有执行此操作

  container.addListener(new ContextLoaderListener(rootContext));

提供部署描述符(web.xml)会很有帮助。再看看这个:谢谢你的回复。我解决了问题,并将答案贴在下面。非常感谢,这解决了我的问题。我在JSF项目中使用Spring、Spring Security、Jersey和Hibernate。在不得不添加jersey-spring4和SpringBridge来解决我的空实体管理器问题之后,我遇到了多上下文加载程序问题,这个解决方案很有效!