Spring @Component, @Service, @Repository and @Controller annotations

We have already seen @Autowired annotation but we have used xml configuration to configure beans and inject it to container but if you use  @Component, @Service, @Repository and @Controller annotations and enable component auto scan, spring will automatically import these bean into container and you don't have to explicitly define them in xml file.
So basically all 4 annotations are used to register bean definition with spring's application context.


@Component is generic annotation for bean definition and registers with application context.

@Service :

@Service is specialised component annotation which is used to annotate classes which belongs to service layer.

@Repository :

@Repository annotation is specialised component annotation which is used to annotate classes at DAO layer. It also makes unchecked exception eligible for conversion into Spring DataAccessException.

@Controller :

@ Controller annotation is specialised component annotation which is used to annotate classes at Presentation  layer. It is widely used in Spring MVC applications.
You should not use @Component annotation unless you are sure that it does not belong to @Service, @Repository and @Controller annotation.

Enable component Scanning : 

All these annotations will work only when you use context:component-scan in applicationcontext.xml. It basically scans for above 4 annotated classes and registers bean with Spring application context.
<context:component-scan base-package="org.arpit.java2blog" />

Spring annotations example : 

In this example,we are going to create country object with help of controller, service and Dao classes by using above annotations.
Lets first create our bean class
package org.arpit.java2blog.bean;

public class Country{
 String countryName; 
 long population;
 public Country() {
 public Country(String countryName,long population) {
  this.countryName = countryName;

 public String getCountryName() {
  return countryName;
 public void setCountryName(String countryName) {
  this.countryName = countryName;
 public long getPopulation() {
  return population;
 public void setPopulation(long population) {
  this.population = population;
package org.arpit.java2blog.controller;

import org.arpit.java2blog.bean.Country;
import org.arpit.java2blog.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller ("countryController")
public class CountryController 
    CountryService countryService;
    public Country createNewCountry()
        return countryService.createNewCountry();

package org.arpit.java2blog.service;

import org.arpit.java2blog.bean.Country;
import org.arpit.java2blog.dao.CountryDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class CountryService {
 CountryDAO countryDAO;
 public Country createNewCountry() {
  return countryDAO.createNewCountry();


package org.arpit.java2blog.dao;

import org.arpit.java2blog.bean.Country;
import org.springframework.stereotype.Repository;

public class CountryDAO {

 public Country createNewCountry() {
  // You should get it from database
  Country country = new Country("√Źndia", 40000);
  return country;


<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="org.arpit.java2blog" />
package org.arpit.java2blog.main;

import org.arpit.java2blog.bean.Country;
import org.arpit.java2blog.controller.CountryController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplicationMain {
 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  CountryController controller = (CountryController) context.getBean("countryController");
  Country country = controller.createNewCountry();
  System.out.println("Country Name : " + country.getCountryName());
  System.out.println("Country's Population : " + country.getPopulation());
When you run above program, you will get below output:
Aug 08, 2016 12:00:42 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c2709da: startup date [Mon Aug 08 00:00:42 IST 2016]; root of context hierarchy
Aug 08, 2016 12:00:42 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Country Name : √Źndia
Country's Population : 40000

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012