Difference between @Component, @Repository and @Service annotations in Spring

In this quick post,, we will learn about the difference between @Component, @Repository and @Service annotations in Spring.

 

Introduction

While working on the enterprise application, we have multi layer or multi module architecture where each module/layer have a specific role to play. In a normal Spring based web application, we can have following layers.

  1. Web layer
  2. Presentation
  3. Service
  4. Data Acess

Above list is the simplistic one and can become complex based on the application. Each layer define their own set of Spring beans to perform the work. Spring Boot use the classpath scanning feature to detect these beans and create/load them. Spring framework provides several annotations I can use which based on the use-case. In this article, we will have a closer look at the difference between @Component, @Repository and @Service annotations in Spring Framework.

On a high level, here is the difference between these 3 annotations:

Annotation Description
@Component Generic stereotype for any Spring-managed component
@Repository Stereotype for persistence layer
@Service Stereotype for service layer

The major difference between these annotations is the classification but at the end they are almost same and we can use one in place of another and can still get our way around.

 

1. @Component

This is a general purpose stereotype annotation available with Spring framework.This annotation shows that the class is a spring managed bean/ component.

@Component
public class ApplicationConfigurations{
    ...
}

[pullquote align=”normal”]Spring only scan @Component and does not look for @Service and @Repository.This happens because they annotate these other annotations themselves with @Component. [/pullquote]

@Component
public @interface Service {
    ….
}
@Component
public @interface Repository {
    ….
}

In another words we can say @Service and @Repository are special types of @Component annotation.

 

2. @Repository

This annotation classifies a Spring bean as a data repository.@Repository annotation also catch platform specific exceptions and re-throw them Spring specific unchecked exception. Spring provides PersistenceExceptionTranslationPostProcessor, class to handle this.

<span class="pun"><</span><span class="pln">bean </span><span class="kwd">class</span><span class="pun">=</span><span class="str">"org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"</span><span class="pun">/></span>

This bean post processor adds an advisor to any bean that’s annotated with @Repository.

 

3. @Service

Java class annotated with @Service annotation shows that the bean is part of the service layer which holds the business logic. This annotation only classify bean as service level beans and have no additional processing like @Repository.

 

Summary

In this short post we saw the difference between @Component, @Repository and @Service annotations in Spring. We talked about each annotation with the area where we should use these annotations. At the end these annotations are almost similar with few additional features but they help us classify the beans and it is always better to choose annotation based on the layer conventions.