【问题标题】:"Cannot initialize context because there is already a root app..." when a Jetty auto-restart occurs after a change in the code Spring Vaadin?“无法初始化上下文,因为已经有一个根应用程序......”当代码 Spring Vaadin 更改后发生 Jetty 自动重启时?
【发布时间】:2015-02-27 20:10:18
【问题描述】:

当我使用 Jetty 运行带有 Vaadin 的 Spring 应用程序时遇到以下问题:

我已将 Jetty 配置为扫描项目工作目录,如果它看到代码中的更改,它会自动重新启动服务器(就像 Tomcat 一样)。

我注意到的是,如果我对代码进行了一点小小的更改,然后 Jetty 会自动重新启动,则会引发以下异常:

java.lang.IllegalStateException:无法初始化上下文,因为已经存在根应用程序上下文 - 检查您的 web.xml 中是否有多个 ContextLoader* 定义!

我知道当有多个 ContextLoaderListener 时会引发此异常,因为两个 ContextLoaderListener 都尝试加载相同的根 ApplicationContext(因此导致异常),也如此处所述:

Why this Spring application with java-based configuration don't work properly

关键是我没有注册另一个 ContextLoaderListener(或者至少,我认为是这样,因为我不能确定因为我通过 Vaadin Spring 插件将 Spring 与 Vaadin 一起使用 -> https://vaadin.com/directory#addon/vaadin-spring 作为 Maven依赖项,并且该插件尚未正式稳定)。

我可以确定的一点是,我的代码中唯一的监听器如下:

package com.app.config;

import javax.servlet.annotation.WebListener;

import org.springframework.web.context.ContextLoaderListener;

@WebListener
public class AppContextLoaderListener extends ContextLoaderListener {
}

所以我想“也许插件有问题?”。但后来我尝试使用 Tomcat 而不是 Jetty 重现该问题(启动 Tomcat,使用Run on Server 在 Tomcat 上运行应用程序,修改了一些任意代码,等待 Tomcat 自动重新发布更改)但 Tomcat 永远不会抛出异常。

所以它可能与 Jetty 相关?有没有人也经历过类似的事情?

问题出在哪里?

这是我使用的applicationContext.xml 文件(为了完整起见,我将其发布):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    <bean class="com.app.config.AppConfig" />
    <context:component-scan base-package="com.app" />
</beans>

编辑:这是异常的完整堆栈跟踪:

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:277)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:764)
    at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:406)
    at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:756)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:242)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1221)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:699)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:454)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:59)
    at runjettyrun.scanner.RJRFileChangeListener.filesChanged(RJRFileChangeListener.java:155)
    at org.eclipse.jetty.util.Scanner.reportBulkChanges(Scanner.java:680)
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:546)
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:398)
    at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:348)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

【问题讨论】:

  • 如果堆栈跟踪有更多内容,请将其包含在您的问题中。
  • @JoakimErdfelt 在编辑中添加了完整的堆栈跟踪

标签: java spring jetty vaadin


【解决方案1】:

您正在使用 run-jetty-run,它不会像您期望的那样重新加载/重新启动。

注意:run-jetty-run 不是一个 jetty 工具或项目,它是一个 3rd 方工具,与 core jetty 没有共同的提交者。

使用 jetty-distribution、jetty-maven-plugin 或 jetty-runner,你会有更好的运气,主要是因为通过 DeploymentManager(run-jetty-run 没有'不使用)。

【讨论】:

  • 谢谢!对于答案,原因和提示的解释!我试试看!
  • 我所承诺的只是“更好的运气”和更正确的行为。并不是说使用这 3 种解决方案中的任何一种都可以解决您的问题。
猜你喜欢
  • 2012-01-19
  • 2016-02-11
  • 2016-12-13
  • 2017-05-09
  • 2016-08-27
  • 1970-01-01
  • 2021-04-03
  • 2011-11-17
相关资源
最近更新 更多