---
title: "Difference between @Component, @Repository & @Service annotations in Spring?"  
description: "Difference between @Component, @Repository & @Service annotations in Spring?"  
author: "Anonymous User"  
published: 2015-07-14  
updated: 2016-07-23  
canonical: https://www.mindstick.com/forum/23338/difference-between-component-repository-service-annotations-in-spring  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Difference between @Component, @Repository & @Service annotations in Spring?

Can @Component, @Repository &[amp](https://www.mindstick.com/forum/156207/what-is-amp); @Service annotations be used interchangeably in [Spring](https://www.mindstick.com/forum/33718/spring-social-facebook-oauth-getting-not-all-data-with-api-v2-5) or do they provide any particular [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) besides acting as a notation [device](https://www.mindstick.com/blog/149/retriving-network-device-information-in-c-sharp)?\
In other words, if I have a [Service class](https://www.mindstick.com/interview/411/what-are-the-main-components-of-wcf) and I change the annotation from @Service to @Component, will it still behave the same way?\
Or does the annotation also [influence](https://yourviews.mindstick.com/story/1553/6-reasons-for-the-western-influence-on-india) the [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) and functionality of the class?

## Replies

### Reply by Alex Theedom

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?](https://readlearncode.com/2016/02/13/insights-from-stackoverflow-most-voted-for-spring-4-questions/#1) Can They Be Used Interchangeable Or Do They Have Specific Functionality?

### Reply by Anonymous User

In Spring 2.0 and later, the @Repository annotation is a marker for any [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) 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 translationAnother 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:\

```
@Component@Scope("prototype")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.


---

Original Source: https://www.mindstick.com/forum/23338/difference-between-component-repository-service-annotations-in-spring

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
