在将Spring启动应用程序部署到tomcat时未调用ApplicationContextInitializer

问题描述 投票:1回答:1

我正在尝试在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 spring-boot
1个回答
1
投票

根据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());
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.