Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What's the difference between @Component, @Repository, @Controller & @Service annotations in Spring?

    • 0
    • 2
    • 0
    • 2
    • 1
    • 0
    • 0
    • 0
    • 2.98k
    Comment on it

    When we define the annotations @Component, @Service, @Controller, @Repository with the classes, Spring automatically scan and identifies those classes and register the Bean definition with the ApplicationContext.

    Difference between @Component, @Repository, @Controller & @Service annotations in Spring:

    1. The main difference between @Component, @Repository, @Controller & @Service annotations is that these are used for different classification.

    2. In our application we can have different layers like presentation, service, business, data access etc. To annotate a class for auto-detection by Spring, we should use the respective annotations as below.

      @Component - generic and can be used across the application. It is a basic auto component scan annotation, it indicates annotated class is a auto scan component.
      @Service - annotate classes at service layer level ( Indicates a Service component in the business layer).
      @Controller - annotated class indicates that it is a controller components, and mainly used at presentation layer.
      @Repository - a data access object (Indicates DAO component in the persistence layer which will act as database repository).

    3. We can use @Component for any auto component scan, but for better readability and better standards we should use respective annotations. @Repository, @Controller & @Service annotations serve as specializations of @Component annotation.

    Example of @Service and @Repository:

    DAO layer

    Define the DAO class as below:

    EmployeeDAO.java

    package com.example.employee.dao;
    @Repository
    public class EmployeeDAO
    {
         @Override
         public String toString() {
         return "Hello , We are in EmployeeDAO";
       }
    }
    

    Service layer

    Define Service class as below:

    EmployeeService.java

    package com.example.employee.services;
    import org.springframework.beans.factory.annotation.Autowired;
    import com.example.employee.dao.EmployeeDAO;
    @Service
    public class EmployeeService
    {
    @Autowired
    EmployeeDAO employeeDAO;
    
    @Override
    public String toString() {
      return "EmployeeService [employeeDAO=" + employeeDAO + "]";
    }
    }
    

    Now define the below code in app-context.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    <context:component-scan base-package="com.example"/>
    </beans>
    

    Now define the below class to run the code:

    App.java

    package com.example.common;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.example.employee.services.EmployeeService;
    public class App
    {
    public static void main( String[] args )
    {
         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"app-context.xml"});
         EmployeeService emp = (EmployeeService)context.getBean("employeeService");
         System.out.println(emp);
    }
    }
    

    Output

     EmployeeService [employeeDAO=Hello , This is EmployeeDAO]
    

    Hope this will help you :)

 1 Comment(s)

  • I think it is worth pointing out that the Repository annotation gives special behavior to all beans it is used on. The PersistenceExceptionTranslationPostProcessor automatically applies persistence exception translation to any thing with the Repository annotation. I found this on a blog post here https://readlearncode.com/2016/02/13/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: