Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

Difference between @Component, @Service, and @Repository Spring Annotations - Java

Hello guys if you are wondering what is the difference between @Component, @Service, and @Reposistory annotation in Spring Framework then you have come to the right place. In the past, I have shared 15 Spring Boot Interview Questions and 30 Spring MVC questions and in this article, I am going to answer the fundamental and popular Spring questions about @Component, @Service, and @Repository annotation, but before we go into differences, let's understand the similarity first. All three of them are used to make a Java class a Spring bean, something which is managed by Spring Framework. 

 Spring 2.0 has @Repository annotation, which is used as marker annotation. Currently a bean marked by @Component is also a Spring bean and a bean marked by @Service is also a spring bean, but they can be differentiated in future to associated Service level responsibility like transaction to a bean which is annotated by @Service, while @Component is a general purpose annotation to mark a Java object as Spring managed bean.


An example of @Component, @Service and @Repository annotation in Spring

As I said, you can use any of these annotation to mark a Java class as Spring managed bean, but using them in right context  is what you need. Annotating a Service class with @Service gives Spring flexibility to do some automation, which is needed by Service classes e.g. transaction management. Currently Spring provides  

Similar to @Repository annotated bean, which provides automatic exception translation for DAO classes.

Here is a simple example to see, where to use @Component, @Service and @Repository annotation is Spring based Java applications. 

Difference between @Component, @Service, and @Repository Spring Annotations - Java


In our example, we have a class called TradeServiceImpl, which is a Service class, is annotated by @Service annotation. Our DAO class TradeDAOImpl is annotated by @Repository and our Domain object or POJO is annotated by @Component.


@Service
public class TradeServiceImpl implements TradeService{

  @Autowired
  private TradeDAO tradeDAO;

   
}

@Repository
public class TradeDAOImple implements TradeDAO{
  
}

@Component
public class Trade{
   
}


That's all about the difference between @Component, @Service, and @Repository Spring Annotations. The key thing is when to use which annotation. So for general purpose just use @Component annotation to mark a Java class as bean but if its part of Service layer then use @Service to allow Spring do some magic for you and if its part of persistence layer then use @Repository for similarly purpose. 


Other Java and Spring Articles and Tutorials you may like to explore

Thanks for reading this article so far. If you like my explanation of difference between Component, Service, and Repository annotation in Spring framework and then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.



5 comments:

  1. There is no answer, only example - transactional for @Service. Does any annotation give additional marks, maybe if we use @Controller it gives some additional annotation specially for REST API? The same question for @Repository annotation, does it provide special annotations or something else to work with repo?

    ReplyDelete
    Replies
    1. Hello @Anonymous, as I said, all three can be used to make a Java class as Spring bean the distinction comes from using the most appropriate annotation for the job. For example @Service is for any class which belongs to service layer and @Repository is for any class which belongs to DAO or Persistence layer.

      Now regarding providing any specific functionality, @Repository provides automatic exception translation for DAO classes, which is not possible if you annotate the bean with @service or @Component.

      Delete
    2. Hi, it looks like the annotations only for developers understanding, just like marks(or comments) and almost don't have any semantic load

      Delete
  2. In the above example, Trade class is marked with @component, generally this class is annotated with @Entity.

    ReplyDelete
    Replies
    1. Yes, if you are using Hibernate or Spring Data JPA, you can also annotate with @Entity

      Delete

Feel free to comment, ask questions if you have any doubt.