更新1:我的JBoss中可能存在一些残留文件(不知道如何)导致它现在正在运行。
我正在寻找我的Spring MVC网络应用程序的100% code-based approach to configuration。当我将它部署到Jetty时,它工作正常。但是,当我将它作为EAR的一部分进行部署时,它会抱怨web.xml中的多个ContextLoader *定义 - 我甚至没有......
我的代码:
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.acme.web.controller"}) //picks up TestController below.
public class WebConfig extends WebMvcConfigurerAdapter {
//It's actually empty
}
public class TestController {
@RequestMapping(value = "/testThisApp")
public String testThisApp(){
return "testThisApp";
}
}
例外:
[org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/app-web]] (MSC service thread 1-7) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: 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:264) [spring-web-3.1.0.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) [spring-web-3.1.0.RELEASE.jar:4.1.7.RELEASE]
at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]