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?
Annotate other components with @Component, for example REST Resource classes.
@Component is a generic stereotype for any Spring managed component.
@Controller, @Service and @Repository are Specializations of @Component for specific use cases.
@Component in Spring
As many of the answers already state what these annotations are used for, we'll here focus on some minor differences among them.
Differences between @Component, @Repository, @Controller and @Service
This is a general-purpose stereotype annotation indicating that the class is a spring component.
What’s special about @Component
<context:component-scan>
only scans@Component
and does not look for@Controller
,@Service
and@Repository
in general. They are scanned because they themselves are annotated with@Component
.Just take a look at
@Controller
,@Service
and@Repository
annotation definitions:Thus, it’s not wrong to say that
@Controller
,@Service
and@Repository
are special types of@Component
annotation.<context:component-scan>
picks them up and registers their following classes as beans, just as if they were annotated with@Component
.They are scanned because they themselves are annotated with
@Component
annotation. If we define our own custom annotation and annotate it with@Component
, then it will also get scanned with<context:component-scan>
This is to indicate that the class defines a data repository.
What’s special about @Repository?
In addition to pointing out that this is an Annotation based Configuration,
@Repository
’s job is to catch Platform specific exceptions and re-throw them as one of Spring’s unified unchecked exception. And for this, we’re provided withPersistenceExceptionTranslationPostProcessor
, that we are required to add in our Spring’s application context like this:This bean post processor adds an advisor to any bean that’s annotated with
@Repository
so that any platform-specific exceptions are caught and then rethrown as one of Spring’s unchecked data access exceptions.The
@Controller
annotation indicates that a particular class serves the role of a controller. The@Controller
annotation acts as a stereotype for the annotated class, indicating its role.What’s special about @Controller?
We cannot switch this annotation with any other like
@Service
or@Repository
, even though they look same. The dispatcher scans the classes annotated with@Controller
and detects@RequestMapping
annotations within them. We can only use@RequestMapping
on@Controller
annotated classes.@Services
hold business logic and call method in repository layer.What’s special about @Service?
Apart from the fact that it is used to indicate that it's holding the business logic, there’s no noticeable speciality that this annotation provides, but who knows, spring may add some additional exceptional in future.
Similar to above, in future Spring may choose to add special functionalities for
@Service
,@Controller
and@Repository
based on their layering conventions. Hence its always a good idea to respect the convention and use them in line with layers.In Spring
@Component
,@Service
,@Controller
, and@Repository
are Stereotype annotations which are used for:@Controller:
where your request mapping from presentation page done i.e. Presentation layer won't go to any other file it goes directly to@Controller
class and checks for requested path in@RequestMapping
annotation which written before method calls if necessary.@Service
: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user not directly call persistence method so it will call this method using this annotation. It will request @Repository as per user request@Repository
: This is Persistence layer(Data Access Layer) of application which used to get data from the database. i.e. all the Database related operations are done by the repository.@Component
- Annotate your other components (for example REST resource classes) with a component stereotype.@Component
is the top level generic annotation which makes the annotated bean to be scanned and available in the DI container@Repository
is specialized annotation and it brings the feature of converting all the unchecked exceptions from the DAO classes@Service
is specialized annotation. it do not bring any new feature as of now but it clarifies the intent of the bean@Controller is specialized annotation which makes the bean MVC aware and allows the use of further annotation like
@RequestMapping
and all suchHere are more details
In Spring 4, latest version:
Repository and Service are children of Component annotation. So, all of them are Component. Repository and Service just expand it. How exactly? Service has only ideological difference: we use it for services. Repository has particular exception handler.