What is the difference between Component, Service, and Repository annotations?
All three mark a class as a Spring bean, and Service and Repository are actually specialisations of Component. Component is the generic marker. Service marks a class holding business logic. Repository marks a class handling data access and also translates database exceptions. They are functionally similar but express intent for different layers.
What each signals
- Component: a generic Spring managed bean.
- Service: the business logic layer, where rules live.
- Repository: the data access layer, talking to the database.
@Service
class UserService { }
@Repository
class UserRepository { }
Repository adds one real behaviour beyond a label: it converts low level database exceptions into Spring's consistent DataAccessException, which makes error handling cleaner.
Say they are all Component under the hood, but you use the specific ones to show which layer a class belongs to and to keep code readable. Mentioning Repository's exception translation is the detail that stands out.
Common follow up questions
Related interview questions
Want the full Spring Boot guide?
Read every Spring Boot concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the Spring Boot guide All Spring Boot questions