michaldo
michaldo

Reputation: 4609

SpringBootServletInitializer#configure is not called for executable WAR

I want to migrate my war application to Spring boot application.

I follow instruction from http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#howto-convert-an-existing-application-to-spring-boot and I made class

@EnableAutoConfiguration
public class MyInitializer extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    application.banner(new MyBanner());
    application.sources(MyEndpoint.class);
    return application;
  }
}

I got deployable WAR.

I next phase I want to get executable WAR. I made class

public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyInitializer.class, args);
    }

}

The problem is that when I call java -jar target/myapp.war MyInitializer#configure is not executed

I'm confused a bit. How to avoid copy-paste logic from MyInitializer to MyApplication. Do I have to join these classes into one?

Upvotes: 5

Views: 5776

Answers (1)

alexbt
alexbt

Reputation: 17045

Personnally I merge both together:

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return MyApplication.build(builder);
    }

    public static void main(String[] args) {
        MyApplication.build(new SpringApplicationBuilder()).run(args);
    }

    private static SpringApplicationBuilder build(SpringApplicationBuilder builder) {
        return builder.banner(new MyBanner()).sources(MyEndpoint.class);
    }
}
  • I really don't think SpringBootServletInitializer will ever be executed when launching your WAR as an executable. Even though the file extension is a WAR, you are really using it as a JAR.

  • When lauched from the command line, the Spring Boot entry point is the main method and SpringBootServletInitializer is just not interpreted as a Spring Boot entry point.

  • SpringBootServletInitializer is only used when deployed as a WAR: the container would use the SpringBootServletInitializer as the Spring Boot entry point (and the main method would be completely ignored).

Upvotes: 14

Related Questions