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.
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{
}
@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.
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?
ReplyDeleteHello @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.
DeleteNow 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.
Hi, it looks like the annotations only for developers understanding, just like marks(or comments) and almost don't have any semantic load
DeleteIn the above example, Trade class is marked with @component, generally this class is annotated with @Entity.
ReplyDeleteYes, if you are using Hibernate or Spring Data JPA, you can also annotate with @Entity
Delete