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 "/*"!
}
}
更新 2これは奇妙です。Tomcat ログは、DeploymentDescriptor クラスの 2 つのインスタンスを識別しているようです。ただし、.war ファイルにこのクラスのインスタンスが 1 つしかないことを確認しました。2番目の(ファントム)インスタンスがどこから来ているのかわかりませんが、少なくともこれで、クラスが2回スキャンされている理由が説明されます...
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]