Junior
What is the difference between @Service, @Repository, and @Controller annotations in Spring?
sobes.tech AI
Answer from AI
These annotations are stereotypical Spring DI annotations used to denote components by their purpose in the architecture of a typical web application:
@Controller: Marks a class as a controller that handles incoming web requests and returns responses. Often used in MVC frameworks.@Service: Marks a class as a service layer component containing business logic.@Repository: Marks a class as a data access component responsible for interacting with a database or other data storage. Spring provides functionality to convert technology-specific exceptions (e.g.,SQLException) into the SpringDataAccessExceptionhierarchy.
At the implementation level, all three annotations are essentially specializations of @Component and ensure bean registration in the Spring context. Their main difference lies in semantics and the functionality provided by Spring.