In this tutorial, we explore the modern approach to bootstrapping Spring web applications using the SpringServletContainerInitializer mechanism. While traditional Servlet web applications relied on web.xml configuration, Servlet 3.0+ specifications introduced programmatic initialization.
The class SpringServletContainerInitializer implements ServletContainerInitializer. That means this class will be loaded and its onStartup() method will be invoked by the Servlet container (version 3.0+) during startup given that spring-web module JAR is present on the classpath.
WebApplicationInitializer
SpringServletContainerInitializer is annotated with @javax.servlet.annotation.HandlesTypes (WebApplicationInitializer.class). That means, its onStartup() method is passed with all classes (on classpath) implementing WebApplicationInitializer. Spring will initialize all such concrete classes and will call their WebApplicationInitializer#onStartup(servletContext) method. These classes are free to do any programmatic servlet component registration and initialization in their onStartup() method.
The client code can directly implement WebApplicationInitializer and can register DispatcherServlet as we saw in getting started example.
Spring abstract WebApplicationInitializer implementations
Spring also provides abstracts implementations of this interface: AbstractDispatcherServletInitializer and AbstractAnnotationConfigDispatcherServletInitializer which already register DispatcherServlet. The client implementation of one of these abstract classes, can further customize the registration process.
AbstractAnnotationConfigDispatcherServletInitializer also initializes AnnotationConfigWebApplicationContext. The client code needs to provide client side configuration classes. Following snippet shows how to implement its abstract methods:
public class AppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses () {
return new Class<?>[]{MyAppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses () {
return null;
}
@Override
protected String[] getServletMappings () {
return new String[]{"/"};
}
}
Complete Example
Controller
package com.logicbig.example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MyMvcController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String prepareView(Model model) {
model.addAttribute(
"msg",
"Spring ServletContainerInitializer Example");
return "my-page";
}
}
The Config
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
}
Implementing The Initializer
package com.logicbig.example;
import org.jspecify.annotations.Nullable;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?> @Nullable [] getRootConfigClasses() {
return new Class[]{MyWebConfig.class};
}
@Override
protected Class<?> @Nullable [] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
Running the app
$ mvn clean jetty:run
$ curl http://localhost:8080/spring-servlet-container-initializer
<html> <body> Message : Spring ServletContainerInitializer Example </body> </html>
Browser
 Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.5 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.5 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- jakarta.servlet.jsp.jstl 3.0.1 (Jakarta Standard Tag Library Implementation)
- JDK 25
- Maven 3.9.11
|