Hibernate DAO 类使用哪个 Spring 注释?
我应该对 Hibernate DAO 类使用什么 Spring 注释,以便可以在扫描过程中找到它们? @Repository、@Service 还是@Component?我无法弄清楚其中的区别。我现在使用的是 Spring 2.5.6。
PS 有人可以指导我快速了解图层的想法吗?我只听说过表示层这样的东西,但没有确切的理解我应该怎么称呼它,什么是业务层?还有其他的吗?
What Spring annotation should I use for Hibernate 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不重要,但
@Repository
是一个不错的选择。 Spring 手册有这样的说法:It doesn't matter much, but
@Repository
is a good bet. The Spring manual has this to say:@Repository
将是我的推荐。表示层意味着 Web UI,因此应该使用
@Controller
注释。服务使用 POJO 接口实现用例;将其标记为
@Service
。控制器将使用服务来完成用例。@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.在核心 Spring 中,我不认为有任何区别。一般来说,这些构造型注释用于
使用基于注释的配置和类路径扫描时的自动检测
(来自 Spring 文档)。可以有一些软件来使用它们,但在没有此类软件的情况下,我选择对我来说最有意义的刻板印象。对于 DAO,我通常选择@Component
,尽管@Repository
也是一个不错的选择。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.