Home Spring Spring3.2 @Controller annotation for defining new controller

Related Tutorials


Share on Google+Share on Google+

@Controller annotation for defining new controller

In this section, you will learn about @Controller annotation for defining new controllers.

@Controller annotation for defining new controller

In this section, you will learn about @Controller annotation for defining new controllers.

@Controller annotation shows that the annotated class act as the controller. This annotation act as role indicator. The DispatcherServlet scans such annotated classes and finds the @RequestMapping annotations in the class so that it can relate the requested URLs to the associated methods.

Prior to Spring 3.0, we need to define the controller in the dispatcher's context  by using standard Spring bean definition. Also, @Controller supports auto detection for component classes in the classpath and auto-registration of bean definitions for these classes.

You need to add the following configuration to enable auto detection of the annotated controllers through component scanning :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

	<context:component-scan base-package="net.roseindia"/>
	<!-- ... -->


You can use @Controller annotation to define new controller class as follows :

public class HelloWorldController {
	public ModelAndView helloWorld() {
		String message = "Hello World, Spring 3.0!";
		return new ModelAndView("hello", "message", message);


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 8, 2013

Related Tutorials

Discuss: @Controller annotation for defining new controller  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image