Difference Between @Component, @Repository, @Service, and @Controller Annotations in Spring (original) (raw)

Last Updated : 10 Jun, 2026

Spring annotations are used to define and manage beans in the Spring IoC container. These annotations help in applying dependency injection and organizing the application into different layers for better structure and maintainability.

****@Component Annotation**

@Component is a generic Spring stereotype annotation used to mark a Java class as a Spring-managed bean so that Spring can automatically detect and register it during component scanning.

It acts as the parent annotation for more specialized stereotypes like @Service, @Repository, and @Controller.

Types Of Component Annotation

@Service **Annotation

@Service is a specialized form of the @Component annotation used to mark a class as a service layer bean in a Spring application. It indicates that the class contains business logic and is automatically detected and managed by the Spring container during component scanning.

@Repository **Annotation

@Repository is a specialized form of the @Component annotation used to mark a class as a data access layer (DAO) component. It indicates that the class is responsible for interacting with the database and performing operations such as storing, retrieving, updating, and deleting data.

@Controller Annotation

@Controller is a specialized form of the @Component annotation used to mark a class as a Spring MVC controller. It handles incoming HTTP requests, processes user input, and returns the appropriate view or response to the client.

Similarity

A class annotated with @Service, @Repository, or @Controller can technically be replaced with @Component, and Spring will still create and manage the bean. However, using the specific stereotype annotation makes the code easier to understand and better represents the class's responsibility.

@Repository vs @Service vs @Controller

Feature @Service @Repository @Controller
**Purpose Used for classes that contain business logic and application services. Used for classes that perform database operations such as CRUD. Used for classes that handle web requests and responses.
**Layer Service Layer Data Access (DAO/Persistence) Layer Presentation/Web Layer
**Role Acts as a service provider. Acts as a DAO (Data Access Object) provider. Acts as a web request handler.
**Main Responsibility Implements business functionality and application logic. Stores, retrieves, updates, deletes, and searches data. Processes HTTP requests and returns views or responses.
**Specialization Of @Component @Component @Component
**Stereotype Type Service Layer Stereotype DAO Layer Stereotype Presentation Layer Stereotype
**Common Usage Business logic classes. Repository/DAO classes. Spring MVC controller classes.
**Example UserService, PaymentService UserRepository, EmployeeDAO UserController, HomeController
**Bean Creation Automatically detected and registered as a Spring bean. Automatically detected and registered as a Spring bean. Automatically detected and registered as a Spring bean.
**Interchangeable? Technically yes, but not recommended. Technically yes, but not recommended. Technically yes, but not recommended.
**Readability Benefit Clearly identifies business logic classes. Clearly identifies data access classes. Clearly identifies request-handling classes.