gpt4 book ai didi

spring - WebApplicationInitializer 被重复调用

转载 作者:行者123 更新时间:2023-11-28 22:03:29 25 4
gpt4 key购买 nike

我有一个基于 Spring 3.1 的应用程序托管在 Tomcat 7.x(最新版本)下。该应用程序仅使用 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 "/*"!
}
}

更新 2 这很奇怪。 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 Overlay 将 crap 一个 Spring xml 配置文件转储到我的应用程序中。无论出于何种原因,这都会导致 WebApplicationInitializer.onStartup() 被调用两次。可能是应用程序上下文和 servlet 上下文的初始化。关闭叠加层,应用程序正在按预期进行初始化。

关于spring - WebApplicationInitializer 被重复调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12808041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com