The Wayback Machine - https://web.archive.org/web/20240813202005/https://www.geeksforgeeks.org/difference-between-component-repository-service-and-controller-annotations-in-spring/
Open In App

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

Last Updated : 09 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. Here, we are going to discuss the difference between the 4 most important annotations in Spring, @Component, @Repository, @Service, and @Controller.

@Component Annotation

@Component is a class-level annotation. It is used to denote a class as a Component. We can use @Component across the application to mark the beans as Spring’s managed components. A component is responsible for some operations. Spring framework provides three other specific annotations to be used when marking a class as a Component. 

  1. @Service
  2. @Repository
  3. @Controller
Types Of Component Annotation

 

To read more about @Component Annotation refer to the article Spring @Component Annotation

A. @Service Annotation

In an application, the business logic resides within the service layer so we use the @Service Annotation to indicate that a class belongs to that layer. It is a specialization of @Component Annotation. One most important thing about the @Service Annotation is it can be applied only to classes. It is used to mark the class as a service provider. So overall @Service annotation is used with classes that provide some business functionalities. Spring context will autodetect these classes when annotation-based configuration and classpath scanning is used.

To read more about @Service Annotation refer to the article Spring @Service Annotation

B. @Repository Annotation

@Repository Annotation is also a specialization of @Component annotation which is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects. Though it is a specialization of @Component annotation, so Spring Repository classes are autodetected by the spring framework through classpath scanning. This annotation is a general-purpose stereotype annotation which very close to the DAO pattern where DAO classes are responsible for providing CRUD operations on database tables. 

To read more about @Repository Annotation refer to the article Spring @Repository Annotation

C. @Controller Annotation

Spring @Controller annotation is also a specialization of @Component annotation. The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It’s used to mark a class as a web request handler. It’s mostly used with Spring MVC applications. This annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.

To read more about @Controller Annotation refer to the article Spring @Controller Annotation

Similarity

One of the interesting queries that arise in front of a developer is, can @Component, @Repository, @Service, and @Controller annotations be used interchangeably in Spring or do they provide any particular functionality? In other words, if we have a Service class and we change the annotation from @Service to @Component, will it still behave the same way? 

So in order to answer the same, it is with respect to scan-auto-detection and dependency injection for BeanDefinition, all these annotations (@Component, @Repository, @Service, and @Controller) are the same. We could use one in place of another and can still get our way around.

By now it is made clear that @Component is a general-purpose stereotype annotation indicating that the class is a spring component and @Repository, @Service, and @Controller annotations are special types of @Component annotation. Now let us finally conclude via plotting the difference table between all annotation types to grasp a better understanding that is as follows:  

@Service Annotation

@Repository Annotation

@Controller Annotation

@Service annotation is used with classes that provide some business functionalities. @Repository Annotation is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects. @Controller annotation indicates that a particular class serves the role of a controller. 
@Service Annotation is a specialization of @Component Annotation. @Repository Annotation is also a specialization of @Component Annotation. @Controller annotation is also a specialization of @Component annotation. 
It is used to mark the class as a service provider. It is used to mark the interface as DAO (Data Access Object) provider. It’s used to mark a class as a web request handler.
It is a stereotype for the service layer. It is also a stereotype for the DAO layer. It is also a stereotype for the presentation layer (spring-MVC).
Switch can be possible. But it is not recommended.  Switch can be possible. But it is not recommended.  We cannot switch this annotation with any other like @Service or @Repository. 
It is a Stereotype Annotation. It is also a Stereotype Annotation. It is also a Stereotype Annotation.

Previous Article
Next Article

Similar Reads

Difference Between @Controller and @Service Annotation in Spring
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. Spring @Controller AnnotationSpring @Controller annotation is a sp
5 min read
Spring Boot - Difference Between @Service Annotation and @Repository Annotation
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. @Service Annotation In an application, the business logic resides
7 min read
Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool Suite
Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. The model object can be passed between view and controller using maps. In this article, we will see how to set up a Spring
5 min read
Difference Between @Controller and @RestController Annotation in Spring
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. @Controller Annotation: Spring @Controller annotation is also a sp
3 min read
Difference Between Spring DAO vs Spring ORM vs Spring JDBC
The Spring Framework is an application framework and inversion of control container for the Java platform. The framework's core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform. Spring-DAO Spring-DAO is not a spring module. It does not provide interfaces or templates
5 min read
Spring Boot Annotations - @JmsListener, @Retryable, @RSocketMessageMapping, @ConstructorBinding, and @Slf4j
Spring Boot is a popular framework for building modern, scalable, and efficient Java applications. One of the key features of Spring Boot is its extensive use of annotations, which simplify the process of configuring and deploying Spring-based applications. In this article, we will explore some of the latest Spring Boot annotations and how they can
7 min read
Spring Boot - @PathVariable and @RequestParam Annotations
When building RESTful APIs with Spring Boot, it's crucial to extract data from incoming HTTP requests to process and respond accordingly. The Spring framework provides two main annotations for this purpose: @PathVariable and @RequestParam. The @PathVariable annotation is used to retrieve data from the URL path. By defining placeholders in the reque
8 min read
Spring Framework Annotations
Spring framework is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. Now talking about Spring Annotation, Spring Ann
5 min read
Spring Core Annotations
Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. In the Spring framework, the annotations are classified into diffe
4 min read
Spring Bean Validation - JSR-303 Annotations
In this article, we'll explore practical examples of how to apply JSR-303 annotations to your domain objects from basic annotations to advanced. So basically annotations provide a declarative way to configure Spring beans, manage dependencies, and define behaviors, reducing the need for boilerplate code and making your code more concise and express
6 min read
Practice Tags :
three90RightbarBannerImg