我正在尝试在spring 4 webmvc应用程序中使用java配置。在Internet上浏览一些示例后,我有以下WebAppApplicationInitializer。
public class AppInit implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "spring.examples.config";
private static final String MAPPING_URL = "/rest/*";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(MAPPING_URL);
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
return context;
}
public class AppInit implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "spring.examples.config";
private static final String MAPPING_URL = "/rest/*";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(MAPPING_URL);
}
private AnnotationConfigWebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
return context;
}
它在jetty,tomcat中运行良好,但是当我使用树脂4.0.40时。 Web服务器显示以下错误:
当我评论这一行时
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 com.caucho.server.webapp.WebApp.fireContextInitializedEvent(WebApp.java:3777)
at com.caucho.server.webapp.WebApp.startImpl(WebApp.java:3687)
at com.caucho.server.webapp.WebApp.access$400(WebApp.java:207)
at com.caucho.server.webapp.WebApp$StartupTask.run(WebApp.java:5234)
at com.caucho.env.thread2.ResinThread2.runTasks(ResinThread2.java:173)
at com.caucho.env.thread2.ResinThread2.run(ResinThread2.java:118)
servletContext.addListener(new ContextLoaderListener(context));
一切正常。
问题是将监听器添加到servlet上下文的目的是什么?将侦听器添加到servlet上下文是否错误?
答案 0 :(得分:1)
问题是树脂4.0.40中的错误http://bugs.caucho.com/view.php?id=5611。 ServletContextListener#contextInitialized()
打了两次电话。在树脂4.0.41中一切正常