Saturday, October 5, 2013

Getting started with annotation based Spring MVC web application

Here is a minimal way to get a Spring 3 MVC project started with Maven.

First create spring-web-annotation/pom.xml file and include the Spring dependency:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""



Now create the Servlet 3 web initializer and the Spring annotation config for the MVC parts in spring-web-annotation/src/main/java/springweb/

package springweb;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[0];

    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{ WebAppConfig.class };

    protected String[] getServletMappings() {
        return new String[]{ "/" };

    public static class WebAppConfig {

The WebApp class extends Spring's built in Servlet3 web initializer code. It allows Servlet3 container such as Tomcat7 to auto detect this web application without the need of web.xml configuration setup. Because of we do not use web.xml, we need this class to allow Spring to hook into the Servlet container to bootstrap their dispatcher servlet. Also instead of typical Spring beans xml file configuration, we now can use all annotation based using WebAppConfig.

Noticed that I have combined the WebAppConfig as inner class, but you can easily move it out as top level class in a full scale application. This is the Spring annotation version of container configuration. You can easily customize the application by adding new @Bean here.

Note: Do not forget to overwrite getServletMappings method with "/", or else your URL request will not direct to the Spring dispatcher for processing! A step that can easily forgotten and you might find your self chasing why Spring controllers are not working.

Above are really the minmal setup you need to start a war project. Next you want to add at least one controller to have some output to verify. Create this controller file spring-web-annotation/src/main/java/springweb/controller/

package springweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

public class IndexController {
    public String index() {
        return "index";

And now you would need the JSP view spring-web-annotation/src/main/webapp/index.jsp

Hello World.

Now cd into spring-web-annotation and execute mvn org.apache.tomcat.maven:tomcat7-maven-plugin:run. You should see your Spring application starting and be able to browse http://localhost:8080/spring-web-annotation URL.

There are lot of cool stuff you can do with Spring MVC. Checkout their awesome docs for more details.