Enterprise Java

@Component vs @Repository vs @Service in Spring

Introduction:

With Spring’s auto-scanning feature, it automatically detects various beans defined in our application. We usually annotate our beans using one of the available Spring annotations – @Component, @Repository, @Service, @Controller.

On detecting the bean, Spring simply registers it into the ApplicationContext.

In this quick tutorial, we’ll look at the difference between @Component, @Repository, and, @Service Spring annotations.

@Component:

We can use @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 using <context:component-scan> tag. During auto-scan, Spring will scan and register all beans marked with a @Component annotation:

@Component
public class Employee {
   ...
 
}

@Repository:

@Repository annotation is a specialization over @Component annotation:

@Component
public @interface Repository {
}

Since @Repository is a type of @Component, Spring also auto-scans and registers them.

@Repository is a stereotype for the persistence layer. Its job is to catch all persistence related exceptions and rethrow them as a Spring DataAccessException.

For this, we should configure PersistenceExceptionTranslationPostProcessor in our application context:

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

This bean post processor adds an advisor to all beans marked with @Repository. The advisor’s responsibility is to translate the platform-specific exceptions to the Spring’s unified unchecked exceptions.

@Service:

Just like @Repository, @Service is another specialization of @Component:

@Component
public @interface Service {
}

Just like @Repository, @Service is also a type of @Component. That means Spring will also automatically detect such beans.

The @Service annotation represents that our bean holds some business logic. Till date, it doesn’t provide any specific behavior over @Component.

Still, we should annotate the service-layer beans with the @Service annotation to make our intent clear. Additionally, we never know if someday Spring chooses to add some specific functionality to it.

Differences in NutShell:

Let’s quickly sum up the differences between @Component, @Repository and, @Service:

  • @Component is the most generic stereotype and marks a bean as a Spring-managed component
  • Both @Service and @Repository annotations are the specializations over the @Component annotation
  • @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
  • It’s always preferable to use @Repository and @Service annotations over @Component, wherever applicable. It communicates the bean’s intent more clearly

Conclusion:

In this mini-tutorial, we have learned the difference between the popular Spring annotations – @Component, @Service, and @Repository.

To conclude, we should always prefer using the annotations based on their specific layer conventions.

Published on Java Code Geeks with permission by Shubhra Srivastava, partner at our JCG program. See the original article here: @Component vs @Repository vs @Service in Spring

Opinions expressed by Java Code Geeks contributors are their own.

Shubhra Srivastava

Shubhra is a software professional and founder of ProgrammerGirl. She has a great experience with Java/J2EE technologies and frameworks. She loves the amalgam of programming and coffee :)
Subscribe
Notify of
guest

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

5 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Chandra gupt
Chandra gupt
4 years ago

Very useful. Thanks

Shubhra
4 years ago
Reply to  Chandra gupt

Glad that you liked it☺️

Andi Kovi
Andi Kovi
4 years ago

Nice article.. Even though you forgot to explain the @Controller which is part of the title of this article..

Narendra Vishwakarma
Narendra Vishwakarma
4 years ago

Very nice and simple explanation. Thanks

Shubhra
4 years ago

Thanks..Glad it helped!

Back to top button