What is the difference between @Component, @Service, @Repository, and @Controller in Spring?
Understand the difference between Spring's @Component, @Service, @Repository, and @Controller stereotype annotations, their roles, and unique behaviors.
Expected Interview Answer
All four are stereotype annotations that mark a class as a Spring-managed bean, but they signal different roles: @Component is the generic base, @Service marks business logic, @Repository marks the data-access layer, and @Controller marks a web request handler.
@Service, @Repository, and @Controller are all specializations of @Component, so they are detected by the same component scan. Beyond documenting intent, @Repository adds a real behavior: it enables persistence exception translation, converting native database exceptions into Spring's DataAccessException hierarchy. @Controller integrates with Spring MVC to map HTTP requests to handler methods. Choosing the specific stereotype improves readability and lets Spring apply layer-appropriate behavior.
- Communicates each class's architectural role clearly
- Enables layer-specific behavior like exception translation
- All are auto-detected by component scanning
- Supports clean separation of concerns
- Allows AOP and tooling to target specific layers
AI Mentor Explanation
In a cricket squad everyone is a player (a @Component), but roles differ: the batter scores runs like a @Service does the real work, the wicketkeeper guards the stumps like a @Repository guards data access, and the captain directs play and talks to officials like a @Controller handles incoming requests. Same team, distinct duties.
Step-by-Step Explanation
Step 1
Start with @Component
It is the generic stereotype that registers any class as a Spring-managed bean via component scanning.
Step 2
Use @Service for logic
Annotate business/service-layer classes to express that they hold application logic.
Step 3
Use @Repository for data
Mark DAO/persistence classes; Spring adds automatic persistence exception translation.
Step 4
Use @Controller for web
Mark Spring MVC handlers so request-mapping annotations route HTTP requests to methods.
Step 5
Rely on component scan
All four are picked up by the same @ComponentScan, so no extra registration is needed.
What Interviewer Expects
- Knowing all three are specializations of @Component
- The unique behavior @Repository adds (exception translation)
- How @Controller ties into Spring MVC request handling
- Why using the specific stereotype aids readability and layering
- That all four are detected by component scanning
Common Mistakes
- Saying they are functionally identical with no differences
- Not knowing @Repository enables persistence exception translation
- Confusing @Controller with @RestController
- Believing @Service adds special runtime behavior
- Forgetting that all three specialize @Component
Best Answer (HR Friendly)
“These are labels that tell Spring what job a class does. @Component is the general label, while @Service marks business logic, @Repository marks database code, and @Controller marks code that handles web requests. Using the right one makes the app easier to read and lets Spring add helpful behavior per layer.”
Code Example
@Repository
public class UserRepository {
// data-access code; gets exception translation
}
@Service
public class UserService {
private final UserRepository repo;
public UserService(UserRepository repo) { this.repo = repo; }
}
@Controller
public class UserController {
private final UserService service;
public UserController(UserService service) { this.service = service; }
}Follow-up Questions
- What extra behavior does @Repository provide over @Component?
- What is the difference between @Controller and @RestController?
- Can you use @Component everywhere instead of the specialized annotations?
- How does component scanning discover these beans?
- How would you register a bean that you cannot annotate directly?
MCQ Practice
1. Which annotation adds automatic persistence exception translation?
@Repository translates native database exceptions into Spring's DataAccessException hierarchy.
2. What is the relationship between @Service and @Component?
@Service, @Repository, and @Controller are all meta-annotated with @Component.
3. Which stereotype marks a Spring MVC web request handler?
@Controller integrates with Spring MVC so request-mapping annotations route requests to methods.
Flash Cards
What is @Component? — The generic stereotype that registers a class as a Spring-managed bean.
What does @Repository add? — Persistence exception translation into Spring's DataAccessException hierarchy, plus data-layer intent.
What does @Service signal? — That the class holds business/service-layer logic; functionally like @Component.
Are the specialized stereotypes related to @Component? — Yes — @Service, @Repository, and @Controller are all specializations of @Component.
Continue Learning
Related Interview Questions
What is the difference between @Controller and @RestController in Spring?
easy
What is @SpringBootApplication and what annotations does it combine?
easy
What is dependency injection in Spring and how does the IoC container work?
medium
What is Spring AOP and how do aspects, advice, and pointcuts work?
medium