@Component,@Service,@Repository,@Controller in spring

By |2018-05-14T03:24:55+05:30December 23rd, 2015|Spring|

In this tutorial, we are going to learn about stereotype annotations in spring framework.

@Component, @Service, @Repository, @Controller  are considered to be the stereotype annotations in spring.

You can also find the advantages of Spring framework along with this tutorial so that you can have a better understanding of spring framework.

Here is the list of spring core stereotype annotations:

Stereotype annotations in Spring Core :

Stereotype annotations were introduced in spring 2.0 version, with the single type @Repository. In Spring 2.5 the spring comes up with the generic stereotype annotation called @Component, this is a continuation for spring 2.0 stereotype annotations with different component types.

As we all know, a component is a generic term. We can term, everything is a component. So that in spring, @Component comes in different flavors like @Service, @Repository, @Controller. Let’s examine the each and every @Component type annotation.

In order to reduce the spring configuration file and to reduce the burden on developers, spring stereotype annotations are introduced.

Spring @Component @Service @Repository

Spring core annotation @Component

@Component is a generic stereotype annotation. We can define @Component annotation on top of a class. If we declare a @Component on top of a class, then that class will be automatically configured by the Spring IOC container, with the unqualified class name as id.

Example:

package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Component;

@Component
public class DemoBean {

}

Spring IOC container will automatically configure the above DemoBean class with id “demoBean”.

In order to tell the spring container about scanning for the components (beans) in a package or its sub packages, we need to configure the component-scan in spring configuration XML like below.

 

<context:component-scan base-package="com.onlinetutorialspoint.beans" />

We can also define the multiple base-packages in component-scan like below:

 

<context:component-scan base-package="com.onlinetutorialspoint.beans, com.onlinetutorialspoint.controllers" />

In order to tell Spring IOC container to process the annotations, we need to configure the information in spring configuration file like below.

<context:annotation-config />

 

Spring Core annotation @Service :

@Service is a sub-annotation of the @Component annotation. We can define the @Service annotation on top of a class. In order to indicate a  class comes under the business layer, we can define that class with @Service annotation.

package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Service;

@Service
public class ProductService {

}

If we declare @Service annotation on top of a class, then it will automatically be configured by IOC container, because @Service annotation is a sub-annotation of @Component annotation.

Spring Core annotation @Repository :

We can define the @Repository annotation on top of a class. In order to indicate a  class comes under the persistent layer, we can define that class with @Repository annotation. @Repository is a sub-annotation of the @Component.


package com.onlinetutorialspoint.beans;

import org.springframework.stereotype.Repository;

@Repository
public class ProductDAOImpl {

}

Spring Core annotation @Controller :

We can define the @Controller annotation on top of a class. In order to indicate a  class comes under the presentation layer, we can define that class with @Controller annotation.


package com.onlinetutorialspoint.controllers;

import org.springframework.stereotype.Controller;

@Controller
public class ProductController {

}

@Controller is a sub-annotation of the @Component annotation, hence it will automatically configure by the Spring IOC container.

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

2 Comments

  1. dhavasanth@gmail.com'
    vasanth May 29, 2016 at 5:33 pm - Reply

    Hello Chandrasekar

    It would be great helpful,if you explain the difference between @Component ,@Service ,@Repository, @Controller or what is the purpose of using these annotation?
    Thanks.

  2. alex.theedom@gmail.com'
    Alex July 23, 2016 at 8:27 am - Reply

    Hello Chandrasekar. Really great article. In order to add something to the conversation and to answer the above poster’s (Vasanth) question. The difference between the annotations are to identify different layers in a tier web application. Components within the persistence, business and presentation layers are annotated respectively by @Repository, @Service and @Component as follows:

    Repository -> Data access components
    Service-> Service components
    Controller -> Controller components
    Component -> any other component

    I decided to write a fuller explanation in a blog post here: https://readlearncode.com/2016/02/13/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1

Leave A Comment