What is @Component vs @Service vs @Repository?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Spring Boot basics — a prerequisite for any developer role.
Answer
All three annotations make a class a Spring-managed bean discovered during component scanning — from Spring's perspective, they are functionally equivalent (all result in a singleton bean in the context). The differences are: @Component: generic stereotype for any Spring-managed component. Use when no specific role applies — utility classes, adapter classes, helpers. @Service: semantically indicates a service layer class — business logic, use cases, orchestration. No additional behavior beyond @Component, but communicates intent to developers and tools. Marks the "application/domain layer." @Repository: semantically indicates a data access layer class. Adds exception translation behavior: Spring adds a PersistenceExceptionTranslationPostProcessor that catches provider-specific exceptions (JdbcSQLException, HibernateException) and translates them to Spring's DataAccessException hierarchy. This makes the exception handling consistent regardless of the underlying ORM/data store. Spring Data repository interfaces (JpaRepository) already include this behavior. Custom JDBC repositories should use @Repository to get translation. Why use the specific stereotypes even if functionally the same? (1) Semantic clarity — documentation of intent; (2) Better Spring tool support (structure analysis); (3) @Repository's exception translation; (4) AOP pointcuts can target specific layers: @Pointcut("within(@org.springframework.stereotype.Service *)"). Best practice: always use the most specific stereotype annotation for your class's role.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.