Card 03/ 07
All 7 cards
ComparisonDifficulty: Intermediate1 min
@Component, @Service, @Repository, @Controller
All four mark a class for component scanning, and all four register a bean the same way. What differs is what each one communicates about the class's role — to a reader, and in one case, to the container itself.
| Annotation | Signals | Mechanical difference from @Component |
|---|---|---|
@Component | A generic, container-managed class | None — it's the base annotation the other three build on |
@Service | Business logic, an application's use cases | None |
@Repository | Data access | Translates persistence-specific exceptions into Spring's own DataAccessException hierarchy |
@Controller | A web layer handler | Recognized by Spring MVC for request mapping |
So the decision rule is about honesty, not behaviour for three of the four: @Component still works everywhere, but @Service on a use case and @Repository on a data-access class tell the next reader what the class is for in a way @Component never does. @Repository's exception translation is the one row here where the choice changes what actually happens at runtime.