Difference Between @Component, @Repository, @Service, and @Controller Annotations in Spring
Last Updated :
09 May, 2022
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.
- @Service
- @Repository
- @Controller
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. |
Similar Reads
How to Create Your First Model in Spring MVC?
Spring MVC is a powerful Web MVC framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
6 min read
How to Create Your First View in Spring MVC?
Spring MVC is a powerful Web MVC Framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
5 min read
Spring MVC CRUD with Example
In this article, we will explore how to build a Spring MVC CRUD application from scratch. CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic operations to create any type of project. Spring MVC is a popular framework for building web applications. Spring MVC follows
7 min read
Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool Suite
Spring MVC framework enables the 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 the view and the controller
5 min read
What is Dispatcher Servlet in Spring?
Spring 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. It made
6 min read
Spring - Shortcut to Create Dispatcher Servlet in Eclipse/Spring Tool Suite
Eclipse is an Integrated Development Environment (IDE) used in computer programming. It includes a base workspace and an extensible plug-in system for customizing the environment. It is the second-most-popular IDE for Java development. Eclipse is written mostly in Java and its primary use is for dev
4 min read
WebApplicationContext in Spring MVC
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. T
7 min read
Spring - Multi Action Controller with Example
Spring 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. It made
4 min read
Spring MVC - Exception Handling
Prerequisites: Spring MVC When something goes wrong with your application, the server displays an exception page defining the type of exception, the server-generated exception page is not user-friendly. Spring MVC provides exception handling for your web application to make sure you are sending your
6 min read
Spring - How to Load Literal Values From Properties File
Literals in Java are a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ââ/count is assigned an integer value in the following statement. int x = 100; // Here 100 is a constant/li
4 min read