What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?

Technology CommunityCategory: SpringWhat’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?
VietMX Staff asked 3 years ago
  • @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.