@Component vs @Service vs @Repository vs @Controller
Fast recall
Quick revision
Core answer
PersistenceExceptionTranslationPostProcessor wraps it in a proxy that translates SQL/JPA/Hibernate exceptions into Spring's DataAccessException hierarchy. `@Controller` enables Spring MVC handler mapping (DispatcherServlet only scans @RequestMapping methods inside @Controller-annotated classes). `@Service` carries no runtime behavior today — it's a semantic marker for business logic used by AOP pointcuts and architecture analysis tools. `@RestController` = @Controller + @ResponseBody on every method, so return values are serialized to JSON directly.Whether the candidate knows these are not just aliases — @Repository has real runtime behavior and the others convey intent used by tools and AOP advisors.
Saying all four are 'exactly the same' and missing the @Repository exception translation and the role of @Controller in Spring MVC dispatch.
Mention that custom stereotypes can be built by composing @Component with your own annotation, and that Spring's persistence exception translation is applied via PersistenceExceptionTranslationPostProcessor.
Interview-ready guide
Interview answer
All four annotations — @Component, @Service, @Repository, @Controller — are meta-annotated with @Component, so component scan discovers them all equally. The difference is not how they are found, it is what each one adds on top of basic bean registration.
`@Repository`
- The only stereotype with real runtime behavior: a
PersistenceExceptionTranslationPostProcessorproxy wraps it - That proxy intercepts Hibernate, JPA, and JDBC exceptions and rethrows them as Spring's
DataAccessExceptionhierarchy - Your service layer catches one consistent exception type regardless of the ORM vendor
- Remove
@Repositoryand exceptions leak as vendor-specific types through the service layer
`@Controller`
- Tells Spring MVC's
DispatcherServletto scan the class for@RequestMappingmethods - Without it, your
@GetMappingendpoint is silently ignored — the most confusing silent failure in Spring MVC @RestController=@Controller+@ResponseBodyon every method
`@Service` and `@Component`
@Servicecarries no runtime behavior — it is a semantic marker for the business logic layer- AOP pointcuts and architecture analysis tools use it to identify the business layer
Start with @Component for generic beans, @Service for business logic, @Repository for data access (and get the exception translation benefit), @Controller for web endpoints. The naming communicates intent to every developer who reads the code.
@Component vs @Service vs @Repository vs @Controller
Direct Answer
All four are stereotype annotations that mark a class as a Spring-managed component and trigger component-scan registration. The key difference: @Repository is the only one with real runtime behavior — PersistenceExceptionTranslationPostProcessor wraps it in a proxy that translates SQL/JPA/Hibernate exceptions into Spring's DataAccessException hierarchy. @Controller enables Spring MVC handler mapping (DispatcherServlet only scans @RequestMapping methods inside @Controller-annotated classes). @Service carries no runtime behavior today — it's a semantic marker for business logic used by AOP pointcuts and architecture analysis tools. @RestController = @Controller + @ResponseBody on every method, so return values are serialized to JSON directly.
Common Mistakes & Pitfalls
Saying all four are 'exactly the same' and missing the @Repository exception translation and the role of @Controller in Spring MVC dispatch.
Check your understanding
Answer these without looking back at the article.
- What is PersistenceExceptionTranslationPostProcessor and when is it active?
- How would you create a custom stereotype annotation composed on top of @Component?
- What is the difference between @Controller and @RestController?
- What happens if you put @RequestMapping on a plain @Component?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.