"Cannot initialize context because there is already a root app..." 代码更改后发生 Jetty 自动重启时 Spring Vaadin?

"Cannot initialize context because there is already a root app..." when a Jetty auto-restart occurs after a change in the code Spring Vaadin?

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

我以扫描项目工作目录的方式配置了 Jetty,如果它发现代码发生了变化,它会自动重启服务器(就像 Tomcat 一样)。

我注意到,如果我对代码进行了很小的更改,然后 Jetty 会自动重启,则会抛出以下异常:

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!

我知道当存在多个 ContextLoaderListener 时会抛出此异常,因为两个 ContextLoaderListeners 都尝试加载相同的根 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 {
}

所以我想"Maybe there's a problem with the addon?"。但后来我尝试使用 Tomcat 而不是 Jetty 重现该问题(启动 Tomcat,运行 Tomcat 上的应用程序 Run on Server,修改了一些任意代码,等待 Tomcat 自动重新发布更改)但是 Tomcat.

永远不会抛出异常

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

问题出在哪里?

这是我使用的 applicationContext.xml 文件(为了完整起见,我 post 它):

<?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.run(Scanner.java:348)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

您正在使用 运行-jetty-运行,reload/restart 与您预期的方式不一样。

注意:运行-jetty-运行 不是码头工具或项目,它是第 3 方工具,没有与核心码头相同的提交者。

要么使用 jetty-distribution,要么 jetty-maven-plugin,要么 jetty-运行ner,你会有更好的运气,主要是因为通过 DeploymentManager(它运行-jetty-运行 不使用).