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:

AnnotationDescription
@ComponentGeneric stereotype for any Spring-managed component
@RepositoryStereotype for persistence layer
@ServiceStereotype 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{
    ...
}

Spring only scan @Component and does not look for @Service and @Repository.This happens because they annotate these other annotations themselves with @Component.

@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.

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

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.

Java Development Journal

Hello!! Welcome to the Java Development Journal. We love to share our knowledge with our readers and love to build a thriving community.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of