Spring 3.1 MVC: xml-free configuration in Servlet 3.0 environment

Starting from Spring 3.1.0.M2 you can configure Servlet Context programatically in Servlet 3.0 environment (Tomcat 7 for example), with no web.xml and no xml at all. This article demonstrate working Hello World example with xml-free web application configuration.

Update: See Spring MVC Quickstart Maven Archetype (no-xml Spring MVC 4 web application): https://github.com/kolorobot/spring-mvc-quickstart-archetype to get started with Spring MVC. The project is actively maintained and just got updated: http://blog.codeleak.pl/2016/01/spring-mvc-4-quickstart-maven-archetype.html

To start with the project I used STS and I created new Template Project (File > New > Spring Template Project > Spring MVC Project). Once the project was created I made some small modification to the POM file:

  • removed Spring Roo dependencies - why they are there - I don't know
  • changed Spring version to 3.1.0.M2 - to have new functionality in place
  • change maven-war-plugin configuration - so the build will not fail on missing web.xml file:
And to the project structure:
  • remove /WEB-INF/web.xml
Note: It is not required to remove web.xml. If you would like to leave it - make sure it looks like this:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

Having project fixed it is time to create configuration files. Let's start with web application initializer that will bootstrap Servlet Context and the Dispatcher Servlet:

public class Initializer implements WebApplicationInitializer {
 public void onStartup(ServletContext servletContext)
   throws ServletException {
  AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();

  ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
    "dispatcher", new DispatcherServlet(mvcContext));
Initializer is going to be automatically bootstrapped by any Servlet 3.0 container. I checked that with Tomcat 7 server. While bootstrapping you will find following log entries:

INFO : org.springframework.web.SpringServletContainerInitializer - Delegating ServletContext to the following WebApplicationInitializer instances: [pl.codeleak.springmvc31demo.config.Initializer@1b4cd65]

Next step is to configure Spring MVC. I do that in following class:

@ComponentScan(basePackages = "pl.codeleak.springmvc31demo.web")
public class MvcConfig {
 public InternalResourceViewResolver configureInternalResourceViewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  return resolver;
@EnableWebMvc annotation used together with @Configuration enables default Spring MVC configuration, equivalent to <mvc:annotation-driven /> . With @ComponentScan annotation we make sure our @Controller will be added to the application context. The configuration class also defines one @Bean: our default view resolver.

The @Controller generated from template I leave with no changes:

public class HomeController {
 private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

 @RequestMapping(value = "/", method = RequestMethod.GET)
 public String home() {
  logger.info("Welcome home!");
  return "home";

Now it is time to run our Hello World example on Tomcat 7 server and see immediate result in the browser:

Hello World!

I must admit I like recent configuration changes in Spring MVC. It is easy to bootstrap the application with no xml files in place. Of course, this is not everything that Spring MVC 3.1.0 brings to the developers. To see more details checkout this post: http://blog.springsource.com/2011/06/13/spring-3-1-m2-spring-mvc-enhancements-2

You may be also interested in getting started with Spring Boot and Thymeleaf using Maven. Spring Boot is a great piece of software allowing you to bootstrap Spring application within a few seconds. Check this post: HOW-TO: Spring Boot and Thymeleaf with Maven


  1. Very nice, I liked it. I don't dislike XML at all, but having a programmatic configuration is way better, of course.

  2. cool stuff... The feature will shorten the xml configuration a alot.

  3. I've tryed your tutorial but I can't get my Servelt 3.0 and Spring MVC 3.1 application to run. Can you please help me out with this error? Or if possible can you share the complete code of this article?

    Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.HandlerMapping org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping()] threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling

    I didn't use a template Spring MVC app from the STS. I don't have any xml files. Even without the STS it still needs to work in a Eclipse J2EE on a Tomcat 7 right...

    Thanks in advance.

  4. Hi Kevin,

    Have a look here: https://github.com/SpringSource/greenhouse/commit/213d2c742d472e602defcde801dd118e098d73c6

    This should be enough to implement the

    I will prepare an working example for this post soon. Stay tuned.

  5. Hi Kevin (again),

    I noticed the same problem while creating new example with Spring 3.1.0.RELEASE:

    And I found the reason. Obvious, like usual.

    mvcContext.refresh(); - remove this line, should work fine now!

    I tested this using this archetype outside Eclipse/STS: https://github.com/kolorobot/spring-mvc-quickstart-archetype

    With couple simple modifications you have your app setup properly, with no web.xml in place!

    Good luck, and thanks for comment!

  6. RB

    Thank you very much for your fast reply and accurate feedback! I managed to make my own MVC application run the way it is supposed to, without any xml files.

    I did indeed needed to remove the "mvcContext.refresh()", but I was wondering why. I'll look into it next morning :D.

    Also! I had to change the

    When I put it only seems to work properly when I use this mapping:

    Again something I'am curious about. I did learn alot thanks to this post, but this is only the beginning. Thank again for helping my getting started with this great new api's.


  7. Hi Kevin,

    Please checkout maven2 archetype I created. It will help you to create the Spring MVC 3.1 project in seconds. Details here: https://github.com/kolorobot/spring-mvc-quickstart-archetype

  8. Hi,

    thanks for the very nice example, it helps me a lot! But now I have a problem, with the error page handling... In the classical web.xml configuration I have the opportunity to set customized error pages for 404 errors.

    Is there also a solution for defining something like that in the initializer class? I searched arround for that problem for some time now, but I found no solution.

    Best regards

  9. This comment has been removed by the author.

  10. how to add the image in this source code

    1. If you use an archetype to create your project, you may put images src\main\webapp\resources\images for example.


Post a Comment

Popular posts from this blog

Different ways of validating @RequestBody in Spring MVC with @Valid annotation