spring-bootservlet-3.0spring-autoconfiguration

Possible Missing Annotation with Spring Boot Initializer


I just generated a WAR packaging app with spring boot initializer and here are the generated sources.

Main application class

@SpringBootApplication
public class ChargingListenerApplication {

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

}

the servlet initializer class.

public class ServletInitializer extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(ChargingListenerApplication.class);
  }

}

How does Spring realize this class and run configure()? Is this just because of the type? If so isn't this a bit weird to handle like this, instead of a proper annotation? Plus, in Spring documentation they extend SpringBootServletInitializer in the main class.


Solution

  • Yes, it is because you are extending SpringBootServletInitializer. It makes use of Spring Framework’s servlet 3.0 support and lets you configure your application when it is launched by the servlet container, which means your custom ServletInitializer.configure() will actually be called.

    And yes, in Spring documentation they extend SpringBootServletInitializer in the main class, but that does not mean that you can't also place it someplace else. The only weird thing is that they do recommend one thing (extending it in the main class) but the initializer does another (creating a separate class), but maybe it is on purpose for clarity reasons.