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 starts and 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.

Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

Leave a Reply

two × = 16

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books