All four are stereotype annotations: each marks a class as a Spring-managed bean found by component scanning. @Service, @Repository and @Controller are specialisations of @Component.
Differences:
- @Component: generic bean with no extra behaviour.
- @Service: business logic layer. No extra behaviour today, but it documents intent and is a natural target for AOP pointcuts (for example, logging or transactions on the service layer).
- @Repository: data access layer. Spring adds exception translation, converting vendor-specific exceptions (JDBC, JPA, Hibernate) into Spring's DataAccessException hierarchy.
- @Controller: web layer in Spring MVC; handler methods return view names. @RestController = @Controller + @ResponseBody, so return values are written to the response body as JSON.
Why it matters:
- Clear layering makes code easier to read and test.
- Tools and aspects can target layers by annotation.
Example answer line: "Functionally they are all components, but @Repository adds exception translation and @Controller enables request mapping, so I use them to make the layers explicit."