Simple Spring MVC with java config – without web.xml

In previous post, we created Spring MVC application with Spring configuration located in Java classes. There was only one xml – web.xml. To get rid off it, and to move configuration from this xml to Java class, we have to add one more class:

package com.devcases.springMVC.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.servlet.DispatcherServlet;

public class AppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));


If we are using maven, we have to disable Maven’s warning about missing web.xml file by adding to pom.xml:


Now our configuration contains no xml’s.

The source code for project can be downloaded from here.

Leave a Reply

Your email address will not be published. Required fields are marked *