Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Spring
Springeasy

`@Component` vs `@Service` vs `@Repository` — what’s the difference?

Tags
#component#service#repository
Back to categoryPractice quiz

Answer

All are stereotypes for component scanning; the main difference is intent. `@Service` marks business logic, `@Repository` marks persistence and can translate persistence exceptions, and `@Component` is generic.

Advanced answer

Deep dive

All three annotations are meta-annotated with `@Component`, so they are discovered by component scanning and registered as beans. The difference is mostly **semantic** (communicating intent), but `@Repository` also enables a practical feature: **persistence exception translation** into Spring’s `DataAccessException` hierarchy.

  • `@Component`: generic stereotype for “a Spring-managed component”.
  • `@Service`: service/business layer (often where you place use-cases and transactions).
  • `@Repository`: persistence/DAO layer (JPA/JDBC) + exception translation.

Why the semantics matter

  • Encourages clean layering (controller → service → repository).
  • Makes AOP pointcuts clearer (e.g., aspects on `@Service` only).

Example

@Repository
class UserDao {
  // JPA/JDBC calls
}

@Service
class UserService {
  private final UserDao dao;
  UserService(UserDao dao) { this.dao = dao; }
}

Common pitfalls

  • Putting business logic into repositories.
  • Assuming `@Service` changes scope/lifecycle (scope is configured separately).
  • Missing `@Repository` on persistence classes and losing consistent exception types.

Related questions

Cloud
Kubernetes Service vs Ingress vs LoadBalancer: what does each do?
#kubernetes#networking#ingress