Spring annotations @Repository and @Service
There are a few reasons:
- It's easier to target pointcuts with more specific annotations. Do not underestimate the usefulness of this!
- The
@Repository
annotation carries with it some additional functionality: it causes exceptions to be wrapped up asDataAccessException
s. - The
@Service
annotation may gain additional semantics in the future but it's not happened yet…
The @Repository
annotation (introduced in Spring 2.0) and @Service
annotation (introduced in Spring 2.5) are specialization of the @Component
annotation.
The main advantage of using @Repository
or @Service
over @Component
is that it's easy to write an AOP pointcut that targets, for instance, all classes annotated with @Repository
.
Also, the specialized annotations help to clearly demarcate application layers (in a standard 3 tiers application).
I believe that there are two things Spring had in mind when they added these Component annotations.
- They can be used to specify point cuts across all objects annotated with that sub-component. (All @Repository or all @Service pointcuts)
- I think Spring also mentioned that they would be adding some future functionality to them. (I have no citation for this, I just think I read this somewhere)