What do @Component, @Service, @Repository and @RestController have in common?
Options
- They are all annotations used only inside test classes
- Stereotypes: Spring detects them and registers beans (Correct option)
- They only work when placed on interfaces, not on classes
- They create the matching database tables at startup
Answer
They are stereotype annotations that tell Spring to detect the class and register it as a bean; the last three add meaning to a layer
Why
@Component is the generic marker: "Spring, please manage this class" (component scanning finds it).
@Service marks business logic, @Repository marks data access (it also translates persistence exceptions into Spring's exceptions), and @RestController marks a web controller that returns data (JSON) instead of views.
They behave like @Component, but names communicate the architecture.
Remember
Stereotypes = "manage this class as a bean" + layer meaning.