Which Spring annotation to use for Hibernate DAO class?
What Spring annotation should I use for Hibernate 开发者_C百科DAO classes so they could be found in scanning process? @Repository, @Service or @Component? I couldn't figure out the difference. I'm on Spring 2.5.6 now.
P.S. Can someone guide me quickly through the layer idea? I only have heard a thing like presentation layer, but don't have exact understanding what should I call so and what is the business layer? Are there other?
@Repository
would be my recommendation.
Presentation tier means web UI, so those should use the @Controller
annotation.
Services implement use cases using POJO interfaces; mark this as @Service
. Controllers will use services to fulfill use cases.
It doesn't matter much, but @Repository
is a good bet. The Spring manual has this to say:
Beginning with Spring 2.0, the @Repository annotation was introduced as a marker for any class that fulfills the role or stereotype of a repository (a.k.a. Data Access Object or DAO)
In core Spring, I don't believe there is any difference. Generally, these stereotype annotations are used for auto-detection when using annotation-based configuration and classpath scanning
(from Spring docs). It is possible to have some software to make use of them but in absence of such software I choose stereotype that makes most sense to me. In case of DAO I usually choose @Component
, although @Repository
is a good option as well.
精彩评论