Is web.xml mandatory in Spring mvc?

Web.xml, also known as deployment descriptor, is traditionally used to define servlets, their mappings, servlet filters, lifecycle listeners and more. Originally it was the only way to provide such configuration. Web. xml defines mappings between URL paths and the servlets that handle requests with those paths.

With the release of the Servlet 3.0 spec it became possible to configure your Servlet Container with no web.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.

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

  • SpringServletContainerInitializer is bootstrapped automatically by any Servlet 3.0 container.
  • SpringServletContainerInitializer looks for classes implementing WebApplicationInitializer .

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.” . It registers a 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.

For bootstrapping the application:

public class CustomWebApplicationInitializer
    extends AbstractAnnotationConfigDispatcherServletInitializer
{

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

}

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.

For the configuration.

@Configuration
@EnableCaching
@EnableWebMvc
@ComponentScan("")
public class WebConfiguration extends WebMvcConfigurerAdapter { ... }

This file is used in place of dispatcher servlet file. Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. To enable autodetection of the annotated controllers, it is required to add component scanning to the configuration. It also gives the path of base package () wherein controller files need to be searched. This class should extend WebMvcConfigurerAdapter class to serve the purpose of dispatcher servlet.

I would also like to higlight that WebMvcConfigurerAdapter should not be confused with WebApplicationInitializer. As its 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: