Home > DeveloperSection > Forums > Difference between @Component, @Repository & @Service annotations in Spring?
Mikki Halpin
Mikki Halpin

Total Post:23

Posted on    July-14-2015 7:14 AM

 Java Java 

 2 Reply(s)
 750  View(s)
Rate this:
Can @Component, @Repository & @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?

In other words, if I have a Service class and I change the annotation from @Service to @Component, will it still behave the same way?

Or does the annotation also influence the behavior and functionality of the class?

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    July-14-2015 8:52 AM

In Spring 2.0 and later, the @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.

Spring 2.5 introduces further stereotype annotations: @Component, @Service, and @Controller. @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.
      Annotation                            Meaning                                             
 @Component                        generic stereotype for any Spring-managed component 
 @Repository                         stereotype for persistence layer                    
 @Service                              stereotype for service layer                        
 @Controller                          stereotype for presentation layer (spring-mvc)      

They are almost the same - all of them mean that the class is a Spring bean. @Service, @Repository and @Controller are specialized @Components. You can choose to perform specific actions with them. For example:

@Controller beans are used by spring-mvc
@Repository beans are eligible for persistence exception translation
Another thing is that you designate the components semantically to different layers.

One thing that @Component offers is that you can annotate other annotations with it, and then use them the same way as @Service.

For example recently I made:

public @interface ScheduledJob {..}
So all classes annotated with @ScheduledJob are spring beans and in addition to that are registered as quartz jobs. You just have to provide code that handles the specific annotation.

Alex Theedom
Alex Theedom

Total Post:1

Posted on    July-23-2016 1:14 AM

This question has been asked many times on internet forums and its the first time I have seen the answer mention that the @Repository annotation confers special behaviour to all beans it marks. To added to the about answer it should be state that it is the PersistenceExceptionTranslationPostProcessor that automatically applies persistence exception translation to any bean marked with @Repository. You might be interested in my article What Is The Difference Between @Component, @Controller, @Repository And @Service Annotations? Can They Be Used Interchangeable Or Do They Have Specific Functionality?

Don't want to miss updates? Please click the below button!

Follow MindStick