0% found this document useful (0 votes)
9 views9 pages

Annotations in Spring

The document explains the differences between four key Spring annotations: @Component, @Service, @Repository, and @Controller. Each annotation serves a specific purpose, with @Service for business logic, @Repository for data access, and @Controller for handling web requests, while all are specializations of @Component. Despite their specific roles, they can be used interchangeably in terms of bean detection and dependency injection.

Uploaded by

srikanth.n24007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Annotations in Spring

The document explains the differences between four key Spring annotations: @Component, @Service, @Repository, and @Controller. Each annotation serves a specific purpose, with @Service for business logic, @Repository for data access, and @Controller for handling web requests, while all are specializations of @Component. Despite their specific roles, they can be used interchangeably in terms of bean detection and dependency injection.

Uploaded by

srikanth.n24007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

1. @Service
2. @Repository
3. @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

@Repository Annotation is used


@Service annotation is @Controller annotation
to indicate that the class
used with classes that indicates that a
provides the mechanism for
provide some business particular class serves
storage, retrieval, update, delete
functionalities. the role of a controller.
and search operation on objects.

@Service Annotation is @Controller annotation


@Repository Annotation is also
a specialization of is also a specialization
a specialization of @Component
@Component of @Component
Annotation.
Annotation. annotation.

It is used to mark the It is used to mark the interface as It’s used to mark a
class as a service DAO (Data Access Object) class as a web request
provider. provider. handler.

It is also a stereotype
It is a stereotype for the It is also a stereotype for the
for the presentation
service layer. DAO layer.
layer (spring-MVC).

We cannot switch this


Switch can be possible.
Switch can be possible. But it is annotation with any
But it is not
not recommended. other like @Service or
recommended.
@Repository.

It is a Stereotype It is also a Stereotype It is also a Stereotype


Annotation. Annotation. Annotation.

A Amiy…

Previous Article Next Article


Difference Between ApplicationContext and Difference Between @Controller and
WebApplicationContext in Spring MVC @Service Annotation in Spring

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…
5 min read

Spring Boot - Difference Between @Service Annotation and @Repositor…


Spring Annotations are a form of metadata that provides data about a
program. Annotations are used to provide supplemental information about a…
7 min read

Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool…
Spring MVC framework enables separation of modules namely Model, View,
and Controller, and seamlessly handles the application integration. This…
5 min read

Difference Between @Controller and @RestController Annotation in…


Spring Annotations are a form of metadata that provides data about a
program. Annotations are used to provide supplemental information about a…
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 b…
5 min read

Spring Boot Annotations - @JmsListener, @Retryable,…


Spring Boot is a popular framework for building modern, scalable, and
efficient Java applications. One of the key features of Spring Boot is its…
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…
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 buil…
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…
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…
6 min read

Spring - Stereotype Annotations


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,…
10 min read

Spring Conditional Annotations


The Spring Framework provides a powerful way to control bean creation and
configuration based on specific conditions through its conditional annotation…
5 min read

Spring Boot - Annotations


Spring Boot Annotations are a form of metadata that provides data about a
spring application. Spring Boot is built on the top of the spring and contains…
8 min read
Difference between service-oriented (SOA) and Micro service Architectu…
1. Service-oriented architecture (SOA) : It is a huge collection of services in
which services communicate with each other. This framework is used for…
5 min read

Spring @Controller Annotation 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,…
4 min read

Spring - REST Controller


Spring Boot is built on the top of the spring and contains all the features of
spring. And is becoming a favorite of developers these days because of its…
4 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,…
5 min read

Spring MVC - Multiple Controller


We may construct numerous controllers at once in Spring MVC. Each
controller class must be annotated with the @Controller annotation. A Sprin…
2 min read

Produce XML Response with Spring WebMVC Controller


In this article, we will explore the mechanisms and aspects of Spring
WebMVC – producing XML responses with controllers. And also guides Java…
3 min read

Spring MVC - Download File Controller


The Spring MVC is an approach for designing and developing the web
application in the MVC pattern. In this article, we will learn how to develop…
4 min read
Spring @Repository Annotation 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,…
5 min read

Spring Boot - Spring JDBC vs Spring Data JDBC


Spring JDBC Spring can perform JDBC operations by having connectivity with
any one of jars of RDBMS like MySQL, Oracle, or SQL Server, etc., For…
4 min read

Spring vs Spring Boot vs Spring MVC


Are you ready to dive into the exciting world of Java development? Whether
you're a seasoned pro or just starting out, this article is your gateway to…
8 min read

Difference between Spring MVC and Spring Boot


1. Spring MVC : Spring is widely used for creating scalable applications. For
web applications Spring provides Spring MVC framework which is a widely…
3 min read

Difference Between Spring Boot Starter Web and Spring Boot Starter…
Spring Boot is built on the top of the spring and contains all the features of
spring. And is becoming a favorite of developers these days because of its…
3 min read

Difference Between Spring MVC and Spring WebFlux


Spring MVCSpring MVC Framework takes on the Model-View-Controller
design pattern, which moves around the Dispatcher Servlet, also called the…
5 min read

Difference between Spring and Spring Boot


Spring Spring is an open-source lightweight framework that allows Java
developers to build simple, reliable, and scalable enterprise applications. Thi…
4 min read
Service Discovery and Service Registry in Microservices
Microservices are small, loosely coupled distributed services. Microservices
architecture evolved as a solution to the scalability, independently deployabl…
5 min read

Spring Boot - Service Class Example for Displaying Response Codes and…
Sometimes the error or status messages in webpages would be different from
the default error message thrown by Tomcat (or any other server). An…
6 min read

Hibernate - Annotations
Annotation in JAVA is used to represent supplemental information. As you
have seen @override, @inherited, etc are an example of annotations in…
7 min read

Article Tags : Difference Between Java Java-Spring

Practice Tags : Java

You might also like