@Component, @Service, @Repository
Pick the right stereotype annotation and know which one adds real behaviour.
Open this lesson in the learning hubKey points
- All three are
@Componentunderneath. Component scan finds them and registers a bean. @Serviceis a pure label for business logic. It adds no behaviour — it adds readability.@Repositorydoes add behaviour: it translates vendor exceptions into Spring'sDataAccessException.@Controllerand@RestControllermark web entry points and enable request mapping.- Rule of thumb: web layer is thin,
@Serviceholds the rules,@Repositoryonly talks to the database.
Example
@RestController // HTTP in, JSON out
class OrderController { }
@Service // business rules, transactions
class OrderService { }
@Repository // data access + exception translation
class JdbcOrderRepository { }
@Component // anything else: a mapper, a validator, a scheduler
class OrderMapper { }
Same wiring, different intent — except @Repository, which really does translate exceptions.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Spring Boot course, and every lesson in it is listed on the Spring Boot contents page.