spring - java.lang.IllegalStateException : Cannot initialize context because there is already a root application context present

标签 spring illegalstateexception applicationcontext

运行我的 web 应用程序时,我每次尝试都会得到下面列出的堆栈跟踪。请注意,据我所知,在 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)

最佳答案

对于任何有类似问题的人 - 事实证明,项目中使用的 spring-jersey 正在设置自己的上下文。我的上下文和 spring-jersey 显然是以随机顺序初始化的。
更多信息在这里:
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 中的第一个参数,确保它在上下文初始化之前设置。

关于spring - java.lang.IllegalStateException : Cannot initialize context because there is already a root application context present,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33633098/

相关文章:

java - 如何统计Firestore中某个集合下的文档数量?

android - 即使在调用notifyDataSetCanged()之后, ListView 也会出现java.lang.IllegalStateException;

java - 获取 java.lang.IllegalStateException : for child view

Android:更改语言 android N,应用程序实例未更新

Spring 确实存在时找不到 bean xml 配置文件

java - Spring 在集成测试中没有点击 ControllerAdvice

java - 如何在 EntityListeners 中注入(inject) EntityManager

java - 使用 Spring Security 玩框架?

java - 我如何正确 Autowiring 此类和 applicationContext.xml?

Java Spring 应用程序 - 向监听器 i 发送上下文初始化事件的异常