@Component vs @Service vs @Repository - Spring

Hello everyone, I hope you all are doing well.Well, today we will discuss the difference between @Component , @Service , and @Repository.

n most typical applications, we have distinct layers like data access, presentation, service, business, etc. And, in each layer, we have sundry beans. Simply put, to detect them automatically,  Spring uses classpath scanning annotations. Then, it registers each bean in the ApplicationContext. Here's an expeditious overview of a few of these annotations: 

  • @Component is a generic stereotype for any Spring-managed component 
  • @Service annotates classes at the accommodation layer 
  • @Repository annotates classes at the assiduousness layer, which will act as a database repository

@Component

We can utilize @Component annotation to mark a bean as a Spring-managed component. In other words, it’s a generic stereotype for any Spring-managed component. 

We can enable an auto-scan utilizing <context:component-scan> tag. During auto-scan, Spring will scan and register all beans marked with a @Component annotation:

@Component
public @interface Service {
}

@Component
public @interface Repository {
}

@Service and @Repository are special cases of @Component. They are technically the same but we utilize them for the different purposes.

@Repository

This is to indicate that the class defines a data repository.In addition to pointing out, that this is an Annotation based Configuration, @Repository’s job is to catch platform specific exceptions and re-throw them as one of Spring’s unified unchecked exception. For this, we’re provided with PersistenceExceptionTranslationPostProcessor, that we are required to add in our Spring’s application context like this:

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

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

@Service

The @Service annotation represents that our bean holds some business logic. Till date, it doesn’t provide any categorical comportment over @Component. Still, we should annotate the service-layer beans with the @Service annotation to make our intent clear.

We can say,
  • @Component is the most generic stereotype and marks a bean as a Spring-managed component
  • @Repository is a stereotype used for persistence layer. It translates any persistence related exceptions into a Spring’s DataAccessException
  • @Service is used for the beans at the service layer. Currently, it doesn’t offer any additional functionality over @Component

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete