我正在尝试在tomcat 8.0.32中部署Spring启动战争。我的ApplicationContextInitializer类定义如下所示。但是,当我启动tomcat时,ApplicationContextInitializer的initialize方法永远不会被调用。我在这里做错了。谢谢你的帮助。
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(Application.class);
app.addInitializers(new ApplicationContextInitializer<?>[] { new DemoInitializer() });
app.run(args);
}
}
根据Spring Boot docs,你需要通过在战争开始时覆盖ApplicationContextInitializer
方法将SpringApplicationBuilder
添加到configure(SpringApplicationBuilder builder)
。你可以让初始化程序同时用于可执行应用程序和战争(其中CustomInitializer
是你的ApplicationContextInitializer
:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return configureApplication(builder);
}
public static void main(String[] args) {
configureApplication(new SpringApplicationBuilder()).run(args);
}
private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
return builder.sources(Application.class).initializers(new CustomInitializer());
}
}