【发布时间】: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 在编辑中添加了完整的堆栈跟踪