Sobes.tech
Middle

What is the difference between @Service, @Repository, and @Controller annotations in Spring Framework?

sobes.tech AI

Answer from AI

These annotations are stereotype annotations in Spring, used to denote components and their roles within the application.

  • @Controller: Marks a class as a web layer controller that handles incoming HTTP requests.
  • @Service: Marks a class as a service layer component containing business logic.
  • @Repository: Marks a class as a data access layer component responsible for interacting with the database.

Although they are functionally similar (being Stereotype, inheriting from @Component), they serve to improve code readability, organize application layers, and can be used by the framework for specialized processing (for example, @Repository automatically participates in translating exceptions from JPA/Hibernate-specific to Spring DataAccessException).

What is the difference between @Service, @Repository… - sobes.tech