Spring MVC without web.xml using WebApplicationInitializer

  • access_time 8 years ago
  • date_range 05/11/2013
  • comment 0
  • share 0
  • label_outlineSpring, Spring MVC

Update 12/2017: Thankfully this is no longer needed, really. Keeping for posterity.

Few months ago I started playing with Spring Framework, which I really enjoy. Looking for the tutorials I found some really great resources on the web as well as books. Unfortunately many of them were written for Spring 2.x, which lacks some recent features, mainly ability to configure everything by Java annotations (a.k.a. JavaConfig) rather than XML. That was something that kept me away from Spring at the first place - doing it programmatically gives much more sense of control and is more readable. So, that's what I'm trying to achieve here, I'll try to show you how to do it in more 'modern' way, showing integrations with various software packages along the way.

Let's start with a simple skeleton Spring MVC application. Since version 3.1 it has Servlet 3 API support, I won't use web.xml for configuring DispatcherServlet, rather I'll configure it programmatically.

Full source code for this article is here on GitHub

Maven configuration

Most important dependencies for this project will be spring-webmvc, which provides DispatcherServlet and pulls other Spring artifacts together with it and Servlet 3.0 API.




AppInitializer is a class that implements WebApplicationInitializer interface. We hook up to onStartup() method, to add DispatcherServlet to ServletContext.

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        return context;


Several things happen here:

  1. AnnotationConfigWebApplicationContext is created. It's WebApplicationContext implementation that looks for Spring configuration in classes annotated with @Configuration annotation. setConfigLocation() method gets hint in which package(s) to look for them.
  2. ContextLoaderListener is added to ServletContext – the purpose of this is to 'glue' WebApplicationContext to the lifecycle of ServletContext.
  3. DispatcherServlet is created and initialized with WebApplicationContext we have created, and it's mapped to "/*" URLs and set to eagerly load on application startup.

Configuration classes

The main AppConfig configuration class doesn't do anything but hits Spring on where to look for its components through @ComponentScan annotation.

@ComponentScan(basePackages = "eu.kielczewski.example")
public class AppConfig {

WebMvcConfig class enables Spring MVC with @EnableWebMvc annotation. It extends WebMvcConfigurerAdapter, which provides empty methods that can be overridden to customize default configuration of Spring MVC. We will stick to default configuration at this time, but it’s advised for you to see what the possibilities are.

public class WebMvcConfig extends WebMvcConfigurerAdapter {


Controllers are annotated with @Controller. It is found by Spring because of @ComponentScan annotation in AppConfig. The method will intercept GET request to "/" to which the response will be sent. @ResponseBody indicates that whatever this method returns will be response body, and in this case it's just a "Hello world" String.

public class IndexController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String showIndex() {
        return "Hello world";



That is all that is needed for skeleton Spring MVC application written using pure JavaConfig and for Servlet 3 API, without web.xml.

See the working example you can play around with - https://github.com/bkielczewski/example-spring-mvc-initializer

Polite Notice - if you have a questions concerning implementation details in your own projects then you're much better off asking them on Stack Overflow. More people to help you this way.