Can @Component
, @Repository
and @Service
annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?
In other words, if I have a Service class and I change the annotation from @Service
to @Component
, will it still behave the same way?
Or does the annotation also influence the behavior and functionality of the class?
From Spring Documentation:
Even if we interchange @Component or @Repository or @service
It will behave the same , but one aspect is that they wont be able to catch some specific exception related to DAO instead of Repository if we use component or @ service
There is no difference between @Component,@Service,@Controller,@Repository. @Component is the Generic annotation to represent the component of our MVC. But there will be several components as part of our MVC application like service layer components, persistence layer components and presentation layer components. So to differentiate them Spring people have given the other three annotations also.
To represent persistence layer components : @Repository
To represent service layer components : @Service
To represent presentation layer components : @Controller
or else you can use @Component for all of them.
all these annotations are type of stereo type type of annotation,the difference between these three annotations are
for example
@Service
or@Repositroy
or@Controller
annotation by default@Component
annotation is going to existence on top of the class@Component is equivalent to
@Service, @Controller, @Repository = {@Component + some more special functionality}
That mean Service, The Controller and Repository are functionally the same.
The three annotations are used to separate "Layers" in your application,
Now you may ask why separate them: (I assume you know AOP-Aspect Oriented Programming)
Let's say you want to Monitors the Activity of the DAO Layer only. You will write an Aspect (A class) class that does some logging before and after every method of your DAO is invoked, you are able to do that using AOP as you have three distinct Layers and are not mixed.
So you can do logging of DAO "around", "before" or "after" the DAO methods. You could do that because you had a DAO in the first place. What you just achieved is Separation of concerns or tasks.
Imagine if there were only one annotation @Controller, then this component will have dispatching, business logic and accessing database all mixed, so dirty code!
Above mentioned is one very common scenario, there are many more use cases of why to use three annotations.
Technically @Controller, @Service, @Repository are all same. All of them extends @Components.
From Spring source code:
Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
We can directly use @Component for each and every bean, but for better understanding and maintainability of a large application, we use @Controller, @Service, @Repository.
Purpose of each annotation:
1) @Controller -> Classes annotated with this, are intended to receive a request from the client side. The first request comes to the Dispatcher Servlet, from where it passes the request to the particular controller using the value of @RequestMapping annotation.
2) @Service -> Classes annotated with this, are intended to manipulate data, that we receive from the client or fetch from the database. All the manipulation with data should be done in this layer.
3) @Repository -> Classes annotated with this, are intended to connect with database. It can also be considered as DAO(Data Access Object) layer. This layer should be restricted to CRUD (create, retrieve, update, delete) operations only. If any manipulation is required, data should be sent be send back to @Service layer.
If we interchange their place(use @Repository in place of @Controller), our application will work fine.
The main purpose of using three different @annotations is to provide better Modularity to the Enterprise application.