WebApplicationInitializer被重复调用
我在Tomcat 7.x(最新版本)下托管了一个基于Spring 3.1的应用程序。该应用程序仅使用Java配置(否web.xml
,没有Spring
XML配置)。所有单元测试都通过了,包括使用Spring Java配置(@ContextConfiguration
)的测试。
问题在于,在部署应用程序时,将 多次
调用WebApplicationInitializer实现。过滤器和侦听器的重复注册会导致异常,并且应用程序永远不会启动。
我不希望WebApplicationInitializer.onStartup()
再被打来电话,如果可能的话,我想消除这种行为。如果有人对为什么会发生这种情况以及如何阻止它提出建议,我将非常感激。
我相信问题出在初始化类本身之外,但是在这里是万一我弄错了…
public class DeploymentDescriptor implements WebApplicationInitializer { private static final Logger LOGGER = LoggerFactory.getLogger("org.ghc.web-app-initializer");
@Override
public void onStartup (ServletContext servletContext) throws ServletException {
// This is the programmatic way of declaring filters. This allows you to order
// Filters. The order of these security filters DOES MATTER!
FilterRegistration.Dynamic mockSecurityFilter = servletContext.addFilter ("mockSecurityFilter", "org.ghc.security.MockSecurityFilter");
mockSecurityFilter.addMappingForUrlPatterns (EnumSet.of (REQUEST), true, "/*");
FilterRegistration.Dynamic siteMinderSecurityFilter = servletContext.addFilter ("siteMinderSecurityFilter", "org.ghc.security.SiteMinderSecurityFilter");
siteMinderSecurityFilter.addMappingForUrlPatterns (EnumSet.of (REQUEST), true, "/*");
FilterRegistration.Dynamic userDetailsStoreFilter = servletContext.addFilter ("userDetailsStoreFilter", "org.ghc.security.UserDetailsStoreFilter");
userDetailsStoreFilter.addMappingForUrlPatterns (EnumSet.of (REQUEST), true, "/*");
// Static resource handling using "default" servlet
servletContext.getServletRegistration ("default").addMapping ("*.js", "*.css", "*.jpg", "*.gif", "*.png");
// Map jspf files to jsp servlet
servletContext.getServletRegistration ("jsp").addMapping ("*.jspf");
// Spin up the Spring 3.1 class that can scan a package tree for classes
// annotated with @Configuration. See org.ghc.spring3.ControllerConfiguration for
// this example.
final AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext ();
dispatcherContext.setServletContext (servletContext);
dispatcherContext.register(ScProviderDirectory.class);
dispatcherContext.refresh ();
// Spin up the Spring DispatcherServlet (just like before) passing the just built
// application context. Load it like the regular Servlet that it is!
final ServletRegistration.Dynamic servlet = servletContext.addServlet ("spring", new DispatcherServlet(dispatcherContext));
servlet.setLoadOnStartup (1);
servlet.addMapping ("/"); // Make sure this is NOT "/*"!
}
}
这只是很奇怪。Tomcat日志似乎标识了我的DeploymentDescriptor类的两个实例。我验证了.war文件中只有该类的一个实例。我不知道第二个(幻像)实例从哪里来,但是至少这解释了
为什么 该类被扫描两次…
logs/localhost.2012-10-09.log:INFO: Spring WebApplicationInitializers detected on classpath: [org.ghc.configuration.DeploymentDescriptor@3b29642c]logs/localhost.2012-10-09.log:INFO: Spring WebApplicationInitializers detected on classpath: [org.ghc.configuration.DeploymentDescriptor@432c4c7a]
回答:
这里的问题是一个Maven覆盖倾倒 垃圾 一个Spring
XML配置文件到我的应用程序。无论出于何种原因,这都会导致WebApplicationInitializer.onStartup()
两次被调用。可能是针对应用程序上下文和servlet上下文的初始化。取消覆盖后,应用程序将按预期进行初始化。
以上是 WebApplicationInitializer被重复调用 的全部内容, 来源链接: utcz.com/qa/403918.html