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?
These are all StereoType annotations. If we placed @controller on top of class. It will not become controller class based on the different layers(components) we can annotate with this annotations but compiler will not do anything about this just for developer understanding purpose we can choose based on the components which annotations we have to write
Use of
@Service
and@Repository
annotations are important from database connection perspective.@Service
for all your web service type of DB connections@Repository
for all your stored proc DB connectionsIf you do not use the proper annotations, you may face commit exceptions overridden by rollback transactions. You will see exceptions during stress load test that is related to roll back JDBC transactions.
Spring provides four different types of auto component scan annotations, they are
@Component
,@Service
,@Repository
and@Controller
. Technically, there is no difference between them, but every auto component scan annotation should be used for a special purpose and within the defined layer.@Component
: It is a basic auto component scan annotation, it indicates annotated class is an auto scan component.@Controller
: Annotated class indicates that it is a controller component, and mainly used at the presentation layer.@Service
: It indicates annotated class is a Service component in the business layer.@Repository
: You need to use this annotation within the persistence layer, this acts like database repository.One should choose a more specialised form of
@Component
while annotating their class as this annotation may contain specific behavior going forward.We can answer this according to java standard
Referring to
JSR-330
, which is now supported by spring, you can only use@Named
to define a bean (Somehow@Named=@Component
). So according to this standard, there seems that there is no use to define stereotypes (like@Repository
,@Service
,@Controller
) to categories beans.But spring user these different annotations in different for the specific use, for example:
aspect-oriented
, these can be a good candidate forpointcuts
)@Repository
annotation will add some functionality to your bean (some automatic exception translation to your bean persistence layer).@RequestMapping
can only be added to classes which are annotated by@Controller
.reference :- Spring Documentation - Classpath scanning, managed components and writing configurations using Java
They are almost the same - all of them mean that the class is a Spring bean.
@Service
,@Repository
and@Controller
are specialized@Component
s. You can choose to perform specific actions with them. For example:@Controller
beans are used by spring-mvc@Repository
beans are eligible for persistence exception translationAnother thing is that you designate the components semantically to different layers.
One thing that
@Component
offers is that you can annotate other annotations with it, and then use them the same way as@Service
.For example recently I made:
So all classes annotated with
@ScheduledJob
are spring beans and in addition to that are registered as quartz jobs. You just have to provide code that handles the specific annotation.