What is the difference between @Component, @Service, @Repository, and @Controller?

Beginner

Answer

These are stereotype annotations for different layers:

  • @Component: Generic stereotype for any Spring-managed component
  • @Service: Indicates service layer components (business logic)
  • @Repository: Indicates data access layer components (DAO)
  • @Controller: Indicates presentation layer components (MVC controllers)

All are essentially @Component with semantic meaning. @Repository provides additional exception translation.