Notification texts go here Contact Us Buy Now!

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

Hello guys if you are wondering what is the difference between @Component, @Controller, @Service, and @Reposistory annotation in Spring Framework then
Aprodesoft
Hello guys if you are wondering what is the difference between @Component, @Controller@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, @Controller, @Service and @Repository annotation is Spring based Java applications. 

Difference between @Component, @Controller, @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.
@Servicepublic class TradeServiceImpl implements TradeService{  @Autowired  private TradeDAO tradeDAO;   }@Repositorypublic class TradeDAOImple implements TradeDAO{  }@Componentpublic class Trade{   }

In short,

@Component is used to indicate that a class is a component. These classes are used for auto detection and configured as bean, when annotation based configurations are used.

@Controller is a specific type of component, used in MVC applications and mostly used with @RequestMapping annotation.

@Repository annotation is used to indicate that a component is used as repository and a mechanism to store/retrieve/search data. We can apply this annotation with DAO pattern implementation classes.

@Service is used to indicate that a class is a Service. Usually the business facade classes that provide some services are annotated with this.

We can use any of the above annotations for a class for auto-detection but different types are provided so that you can easily distinguish the purpose of the annotated classes.

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.

About the Author

Aprodesoft
saya seorang yang bisa diadalakan dalam mencari rekomendasi atau informasi yang bagus untuk anda.

Posting Komentar

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.