Annotations in Spring
Annotations 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. Here, we are going to discuss the
difference between the 4 most important annotations in Spring, @Component,
@Repository, @Service, and @Controller.
@Component Annotation
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
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.
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?
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).
A Amiy…
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
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 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
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