Repaso
Español

What do @Component, @Service, @Repository and @RestController have in common?

Options

  1. They are all annotations used only inside test classes
  2. Stereotypes: Spring detects them and registers beans (Correct option)
  3. They only work when placed on interfaces, not on classes
  4. 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.

Key terms

Practice this question in Repaso