Difference Between @Component, @Repository, @Service, and @Controller Annotations in Spring

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.

  • They help in building a layered architecture (Controller, Service, Repository).
  • They improve code readability and separation of concerns.
  • All four annotations are used for Spring bean registration.

@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 enables component scanning and automatic bean creation in Spring IoC container.
  • It is a base annotation for specialized stereotypes.
  • Used when a class does not clearly fit into Service/Repository/Controller category.
  • Helps in loose coupling and dependency injection. 

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.

  • Used to represent the business/service layer of an application.
  • It is a specialized version of @Component.
  • Can be applied only on classes.
  • Spring automatically creates and manages its bean.

@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.

  • Used for the persistence/data access layer of an application.
  • It is a specialized version of @Component.
  • Automatically detected and managed by the Spring container.
  • Commonly used with DAO and Repository classes.

@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.

  • Used for the web/presentation layer of an application.
  • It is a specialized version of @Component.
  • Handles incoming HTTP requests and user interactions.
  • Commonly used with @RequestMapping, @GetMapping, and @PostMapping.
  • Automatically detected by Spring during component scanning.

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
PurposeUsed 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.
LayerService LayerData Access (DAO/Persistence) LayerPresentation/Web Layer
RoleActs as a service provider.Acts as a DAO (Data Access Object) provider.Acts as a web request handler.
Main ResponsibilityImplements 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 TypeService Layer StereotypeDAO Layer StereotypePresentation Layer Stereotype
Common UsageBusiness logic classes.Repository/DAO classes.Spring MVC controller classes.
ExampleUserService, PaymentServiceUserRepository, EmployeeDAOUserController, HomeController
Bean CreationAutomatically 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 BenefitClearly identifies business logic classes.Clearly identifies data access classes.Clearly identifies request-handling classes.
Comment