【问题标题】:java.lang.IllegalStateException: Cannot initialize context because there is already a root application context presentjava.lang.IllegalStateException:无法初始化上下文,因为已经存在根应用程序上下文
【发布时间】:2016-02-11 12:20:08
【问题描述】:

运行我的 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 illegalstateexception applicationcontext


【解决方案1】:

对于任何有类似问题的人 - 原来项目中使用的 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 中的第一个参数,确保在初始化上下文之前设置它。

【讨论】:

  • 非常感谢,这解决了我的问题。我在我的 JSF 项目中使用 Spring、Spring Security、Jersey 和 Hibernate。在不得不添加 jersey-spring4 和 spring-bridge 来解决我的空实体管理器问题后,我遇到了多个上下文加载器问题,这个解决方案有效!
【解决方案2】:

如果你使用的是spring boot,请确保你没有这样做

  container.addListener(new ContextLoaderListener(rootContext));

【讨论】:

    猜你喜欢
    • 2012-01-19
    • 2016-12-13
    • 2017-05-09
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多