When use AbstractAnnotationConfigDispatcherServletInitializer and WebApplicationInitializer? When use AbstractAnnotationConfigDispatcherServletInitializer and WebApplicationInitializer? spring spring

When use AbstractAnnotationConfigDispatcherServletInitializer and WebApplicationInitializer?


With the release of the Servlet 3.0 spec it became possible to configure your Servlet Container with (almost) no xml. For this there is the ServletContainerInitializer in the Servlet specification. In this class you can register filters, listeners, servlets etc. as you would traditionally do in a web.xml.

Spring provides a an implementation the SpringServletContainerInitializer which knows how to handle WebApplicationInitializer classes. Spring also provides a couple of base classes to extend to make your life easier and the AbstractAnnotationConfigDispatcherServletInitializer is one of those. It registersa ContextLoaderlistener (optionally) and a DispatcherServlet and allows you to easily add configuration classes to load for both classes and to apply filters to the DispatcherServlet and to provide the servlet mapping.

The WebMvcConfigurerAdapter is for configuring Spring MVC, the replacement of the xml file loaded by the DispatcherServlet for configuring Spring MVC. The WebMvcConfigurerAdapter should be used for a @Configuration class.

@Configuration@EnableWebMvcpublic class WebConfiguration     extends WebMvcConfigurerAdapter implements WebApplicationInitializer{ ... }

I wouldn't recommend mixing those as they are basically 2 different concerns. The first is for configuring the servlet container, the latter for configuring Spring MVC.

You would want to split those into 2 classes.

For the configuration.

@Configuration@EnableWebMvcpublic class WebConfiguration extends WebMvcConfigurerAdapter { ... }

For bootstrapping the application.

public class MyWebApplicationInitializer    extends AbstractAnnotationConfigDispatcherServletInitializer{    protected Class<?>[] getRootConfigClasses() {        return new Class[] {RootConfig.class};    }        protected Class<?>[] getServletConfigClasses()  {        return new Class[] {WebConfiguration .class};    }        protected String[] getServletMappings() {        return new String[] {"/"};    }}

An added advantage is that you now can use the convenience classes provided by Spring instead of manually configuring the DispatcherServlet and/or ContextLoaderListener.


To start from the beginning it is worth looking into how servlet container starts.

So to start - SpringServletContainerInitializer has to find the right class implementing WebApplicationInitializer. There are two ways of making it happen:

  1. One is by implementing WebApplicationInitializer on its own; the interface was introduced in Spring 3.1
  2. The second is by extending AbstractAnnotationConfigDispatcherServletInitializer class which also implements WebApplicationInitializer. The class was introduced in Spring 3.2 for convenience and it is "the preferred approach for applications that use Java-based Spring configuration." - see the link. It enables you to start servlet application context as well as root application context.

I would also like to higlight that WebMvcConfigurerAdapter you mention should not be confused with WebApplicationInitializer. As it name suggests - it has to do with configuring "Mvc". It is an adapter class that implements empty methods from WebMvcConfigurer. You use it when you configure your Mvc controller with @EnableWebMvc annotation.

Hope this helps.