Spring MVC annotation example

spring mvc annotation example

Spring MVC annotation example

We already have created an example using XML configuration. Now we will create hello world example using spring annotation.

Spring annotation is good if you want to avoid writing long XML for configuration. Spring annotation provides shortcut method for performing particular action.

For example, using only @Configuration tells container that this particular file is a configuration file.

So let’s start creating an example.

Step 1 : Setting up environment

First thing first, we need to setup our environment in order to create some good example. So if you have not done setup for spring. You can follow this Setting Up Environment And Sample Project tutorial.

With the help of this tutorial, we will be able to setup required resources for spring, maven and tomcat on your local machine as well as you will be able to create sample maven project.

Step 2 : Directory Structure

It is important to understand the directory structure of the project. Below you will find the directory structure which I am using for the project.

spring mvc annotation example

Step 3 : Annotation Bean Configuration

You can create annotation based configuration with the help of @Configuration annotation. By using @Configuration container will be able to scan for configuration files which is having this annotation.

package com.testspring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@ComponentScan(basePackages = "com.testspring") 
public class ApplicationConfig extends WebMvcConfigurerAdapter {
 @Bean(name = "SpringAnnotation") public ViewResolver viewResolver() {
 InternalResourceViewResolver view = new InternalResourceViewResolver();
   return view;

I will discuss every annotation and class used by configuration.

@Configuration Applying class level @Configuration annotation to java class indicates that it’s primary purpose to provide bean definitions and inter bean dependencies.

@EnableWebMvc To enable Web MVC support, we can use @EnableWebMvc annotation in java file. Which enable web MVC validation and configuration support.

@ComponentScan The @ComponentScan annotation scan for the provided packages for component files. Component files can be @Component, @Controller. It’s basePackages attribute takes package name as input.

WebMvcConfigurerAdapter If you want to customize web configuration, you can simply implement WebMvcConfigurer or you can also extend WebMvcConfigurerAdapter. You can override the methods provided by WebMvcConfigurerAdapter as per your requirement. For example, you can provide your own custom validator using getValidator() method or you can provide your own message code resolver using getMessageCodesResolver() method etc.

@Bean You can create bean by simply using @Bean annotation. Also you can provide name of the bean using name attribute of @Bean annotation.

ViewResolver and InternalResourceViewResolver When controller process the request, It returns two component one is View name and another is Model data. Every method in controller must resolve with some view name. InternalResourceViewResolver implements ViewResolver which provides us the facility to configure how view should be resolved.
InternalResourceViewResolver is subclss of UrlBasedViewResolver in support for Servlet, JSPs. You can also specify view class name using setViewClass() method which will generate view.

Step 4 : Create Application Initializer

Next step is to create the application initializer which will scan for the bean configuration classes. WebApplicationInitializer is the interface which ensures that your code based configuration is detected and used to initialize the spring container.

An abstract class implementation of this interface AbstractAnnotationConfigDispatcherServletInitializer makes even easier to configure DispatcherServlet and ServletMapping as below :

package com.testspring.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

 protected Class<?>[] getServletConfigClasses() {
 return null;

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

You can configure you bean definition either in root context or servlet context with the help of getRootCofingClasses() or getServletConfigClasses(). Also you can provide the servlet mapping using getServletMappings() method.

Step 5 : Create Controller We already have discussed about the controller in this Spring Hello World XML Example tutorial. Please read this tutorial for further reference.

Step 6 : Create View

Below is the view file inside WEB-INF/views/ home.jsp
 <h2>Spring MVC Hello World Example using Annotation configuration Metadata</h2>

Step 7 : output

Spring MVC annotation example


Related Post

Leave a Reply

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