Spring @Component vs @Repository vs @Service vs @Controller

preview_player
Показать описание
@Component
Indicates that an annotated class is a “component”.
It is a generic stereotype for any Spring-managed component.
At component can be used in any Layer.

@controller
Indicates that an annotated class is a “Controller”.
Used to make a class as a Spring Web MVC controller.
It is used in the web layer.

@Service
Indicates that an annotated class is a “Service”.
It is used in the application business logic layer
You can use @Service over @Component in service-layer classes.

@Repository
Indicates that an annotated class is a “Repository”.
It is used in the database layer.
It imports the DAOs into the DI container.
It makes the unchecked exceptions eligible for translation into Spring DataAccessException.
This is where the database connects and interactions happen.
Рекомендации по теме