Java Zero to All All questionsPractise free
Interview questions › Spring & Spring Boot › IoC, DI & Bean Lifecycle

What's the difference between @Component, @Service, @Repository, and @Controller? Does the framework treat them differently?

Level II · JuniorIoC, DI & Bean Lifecycle
Short answer

All four are stereotype specializations of @Component and are equally picked up by component scanning, so functionally they mostly just document intent.

Explanation

The real exception is @Repository: it enables persistence exception translation, converting vendor-specific SQLExceptions into Spring's unchecked DataAccessException hierarchy via a PersistenceExceptionTranslationPostProcessor. @Controller/@RestController are recognized by Spring MVC's handler mapping. Using the precise stereotype also lets AOP pointcuts and tooling target layers cleanly.

Common mistakes
  • Saying the four annotations are completely identical.
  • Not knowing @Repository adds exception translation.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for IoC, DI & Bean Lifecycle.

Practise free

More on IoC, DI & Bean Lifecycle