Java 在Spring中将侦听器添加到servlet上下文
我试图在Spring4WebMVC应用程序中使用java配置。在网上浏览了一些示例之后,我有了以下WebAppApplicationInitializerJava 在Spring中将侦听器添加到servlet上下文,java,spring,servlets,resin,Java,Spring,Servlets,Resin,我试图在Spring4WebMVC应用程序中使用java配置。在网上浏览了一些示例之后,我有了以下WebAppApplicationInitializer public class AppInit implements WebApplicationInitializer { private static final String CONFIG_LOCATION = "spring.examples.config"; private static final String MAP
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:无法初始化上下文,因为存在
已存在根应用程序上下文-检查是否有
web.xml中有多个ContextLoader*定义!
位于org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:277)
位于org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
位于com.caucho.server.webapp.webapp.FireContextInitializeEvent(webapp.java:3777)
位于com.caucho.server.webapp.webapp.startImpl(webapp.java:3687)
com.caucho.server.webapp.webapp.access$400(webapp.java:207)
位于com.caucho.server.webapp.webapp$StartupTask.run(webapp.java:5234)
位于com.caucho.env.thread2.ResinThread2.runTasks(ResinThread2.java:173)
位于com.caucho.env.thread2.ResinThread2.run(ResinThread2.java:118)
当我评论这句话时
一切正常
问题是将侦听器添加到servlet上下文的目的是什么?不将侦听器添加到servlet上下文是错误的吗?问题在于resin 4.0.40中的错误<代码>ServletContextListener#contextInitialized()调用了两次。在resin 4.0.41中,一切正常为什么不通过web.xml定义监听器,让容器来处理它?我希望web应用程序没有任何xml配置,您能用抛出它的类显示完整的错误消息吗?Serge,我在stacktrace中添加了错误
servletContext.addListener(new ContextLoaderListener(context));