Skip to content

Step by step spring mvc app using java config

July 1, 2013

Spring 3.0 provides option of configuration via java code. With out going into theory or advantages or disadvantages I will get into steps directly.

Step 1 : Create simple web  application using maven

mvn archetype:generate -DgroupId=org.opencab
    -DartifactId=cabms -DarchetypeArtifactId=maven-archetype-webapp

Step 2: Convert solution to eclipse project

mvn eclipse:clean eclipse:eclipse -DdownloadSources=true
    -DdownloadJavadocs=true -Dwtpversion=2.0

Step 3: Create directory structure under src\main\java folder. rest is created by maven.

── pom.xml
── src
├── main
│   ├── java
│   │   └── org
│   │       └── opencab
│   │           ├── config
│   │           │   ├──
│   │           │   └──
│   │           └── controller
│   │               └──
│   ├── resources
│   │   └──
│   └── webapp
│       ├── index.jsp
│       └── WEB-INF
│           └── web.xml
└── test

Step 4: Modify pom.xml

<project xmlns="" xmlns:xsi=""
<name>cabms Maven Webapp</name>



<!-- Servlet -->


Step 5 : Implement WebApplicationInitializer in your class and override onStartup method.

package org.opencab.config;

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

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

public class Initializer implements WebApplicationInitializer {

public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
servletContext.addListener(new ContextLoaderListener(ctx));


Dynamic servlet = servletContext.addServlet("dispatcher",
new DispatcherServlet(ctx));



Step 6 : Write webconfig

package org.opencab.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@ComponentScan(basePackages = "org.opencab")
@PropertySource({ "" })
public class WebAppConfig {
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
return resolver;

Step 7 : Write simple hello controller

package org.opencab.controller;

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

public class HelloController {

@RequestMapping(value = "/")
public String home() {
System.out.println("HomeController: Passing through...");
return "home";

That’s all you need to bring up simple application using spring javaConfig.
Note that web.xml file will to empty, since all the configuration are moved into java classes.
Full source code is available at

From → Tech

  1. alberto permalink

    thanks for your tuto. When I setup a project using xml files, I have web.xml and servlet-xxx.xml .
    which file ( and is equivalent of web.xml et servlet-xxx.xml ?
    and how can I map a ressource ( css and js files) like this in xml file ?


  2. Correct — web.xml servlet-xxx.xml

    for resource config
    refer to –

    • alberto permalink

      I had in
      public void addResourceHandlers(ResourceHandlerRegistry registry) {


      In jsp file:

      style.css file doesn’t found again

      • can post your jsp file, I hope you are using /css/style.css ? enable debug logs in server and see what path it is looking while trying to display your jsp .

    • alberto permalink

      this is my jsp file:

      Home page

      Home page

      <!– Add new contact –>
      Les flux

  3. alberto permalink


    Home page

    Home page

    <!– Add new contact –>
    Les flux


  4. alberto permalink

    hi I can’t paste correctly html code here.

    I fix this issue using :
    – WebAppConfig extends WebMvcConfigurerAdapter
    – in WebAppConfig
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

  5. Hey Rajender,

    thanks for this tutorial!

    I have – maybe a stupid – question (I am a J2EE/Spring beginner):
    I can deploy it on a tomcat but how i can test if it works?


    Best Regards

  6. Just create the project in eclipse and configure eclipse to run with tomcat. Try these steps to configure your eclipse with tomcat.

  7. how configure filters for specific URLs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: