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

Beginner

Answer

All are specializations of @Component for specific layers:

  • @Component: Generic stereotype for any Spring-managed component
  • @Service: Business logic layer components
  • @Repository: Data access layer components (adds exception translation)
  • @Controller: Presentation layer components for handling HTTP requests
@Service
public class UserService { }
@Repository
public class UserRepository { }
@Controller
public class UserController { }

Functionally similar, but provide better semantic meaning and potential for additional features.