How to use Spring’s WebApplicationInitializer

by | Jan 17, 2014

The DispatcherServlet has always served as the doorway component or “front controller” in a Spring Framework Web MVC application.  It’s job has been and still is to marshal requests to other components in the Spring MVC application backend and to orchestrate a response back to clients.

DispatcherServlet and the web.xml

Until Spring 3.1, the DispatcherServlet had to be registered with the Servlet container via the standard Web application web.xml file. The following elements had to be added to register the DispatcherServlet.

<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.request</url-pattern>
</servlet-mapping>

With the presence of the Servlet 3.0 specification, the web.xml file in a Web application is now optional.  The Servlet 3.0 API brought annotations to the Java Web application world allowing the registration of servlets to be accomplished by annotation.

However, since you don’t write the DispatcherServlet, and the Spring community didn’t have the knowledge and details of your application to be able to annotate the DispatcherServlet for you, there was a bit of a quandary.  How do you get the DispatcherServlet registered with the Web container without web.xml?

The WebApplicationInitializer

The answer was provided in Spring 3.1 (and of course is available with Spring 4 which came out in December 2013) with the WebApplicationInitializer.  An implementation of the WebApplicationInitializer interface configures the ServletContext programmatically.  In particular, it allows for the creation, configuration, and registration of the DispatcherServlet programmatically.  Thereby allowing the web.xml file to be removed from modern Spring MVC applications.  A simple implementation of WebApplicationInitializer is shown below.

public class WebMVCApplicationInitializer implements 
  WebApplicationInitializer {

    public void onStartup(ServletContext container) {
      ServletRegistration.Dynamic registration = 
        container.addServlet("dispatcher", new DispatcherServlet());
      registration.setLoadOnStartup(1);
      registration.addMapping("*.request");
    }
}

When you need to customize the associated WebApplicationContext (i.e. the Spring container), you may prefer to build an instance of the Spring container in the WebApplicationInitializer and provide the container to the DispatcherServlet.

public class WebMVCApplicationInitializer implements 
  WebApplicationInitializer {

    public void onStartup(ServletContext container) {
      XmlWebApplicationContext appContext = 
        new XmlWebApplicationContext();
      ServletRegistration.Dynamic registration = 
        container.addServlet("dispatcher", 
        new DispatcherServlet(appContext));
      registration.setLoadOnStartup(1);
      registration.addMapping("*.request");
    }
}
Wrap Up

With the WebApplicationInitializer, Spring Framework Web MVC applications can now rid themselves of web.xml.  Additional configuration/implementations are available for programmatically registering the DispatcherServlet and configuring the Spring container – see here for more details.