SpringBootServletInitializer connects a Spring Boot application to a traditional servlet container. It configures DispatcherServlet and binds Spring beans of type Filter, Servlet, or ServletContextInitializer to the container. section 18.4
Use it when packaging the application as a WAR. Create a subclass, override configure(), and register the main Spring configuration class through SpringApplicationBuilder. A class annotated with @SpringBootApplication can then discover additional configuration through component scanning. section 18.4
Servlet initializer for Taco Cloud
TacoCloudServletInitializer extends SpringBootServletInitializer and returns builder.sources(TacoCloudApplication.class) from configure(). The application class acts both as the executable-JAR bootstrap class and the registered Spring configuration class. section 18.4
Listing 18.1 Enabling Spring web applications via Java
package tacos;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
public class TacoCloudServletInitializer
extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(TacoCloudApplication.class);
}
}
View the complete Java initializer, including the SpringApplicationBuilder, subclass declaration, and configure() override. section 18.4
A typical deployment choice follows this pattern:
- Choose WAR packaging when deploying into a traditional application server; Spring Boot projects generated with the WAR option already include the initializer. section 18.4
- Use an executable JAR when the deployment environment does not require a traditional servlet container. section 18.6
