Spring Boot FAQ: Top Questions
15. What is the difference between @Component, @Service, @Repository in Spring Boot?
All three annotations are used to mark a class as a Spring-managed component. The difference lies in their semantics and usage.
📘 Breakdown:
- @Component: Generic stereotype for any Spring-managed component.
- @Service: Used in service layer for business logic.
- @Repository: Used in persistence layer, supports exception translation.
📥 Example:
@Service
public class UserService {}
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
🏆 Expected Output:
Beans registered and auto-wired in the Spring context.
🛠️ Use Cases:
- Clear layering of application structure.
- Separation of concerns.