在dao中将hibernate与spring集成在一起,而无需依赖spring [英] integrate hibernate with spring without spring dependency in dao

查看:51
本文介绍了在dao中将hibernate与spring集成在一起,而无需依赖spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将hibernate与spring集成时,通常会实现@Repository spring注释使用的基于注释的方法.我了解到这样做的目的是为了消除dao中的spring依赖关系,并且因为hibernate3支持上下文会话来管理会话

When we integrate hibernate with spring we normally implement annotation based approach use by @Repository spring annotation. I learned the purpose of going for it is, in order to eliminate the spring dependency in our dao and since hibernate3 support contextual session to manage sessions

@Repository
public class HibernateSpitterDao implements SpitterDao{

privateSessionFactorysessionFactory;

@Autowired
public HibernateSpitterDao(SessionFactory sessionFactory){
  this.sessionFactory=sessionFactory;
}

private SessioncurrentSession(){
  return sessionFactory.getCurrentSession();
}
...
}

例如,如果我们不使用基于注释的方法,则dao将直接依赖于spring特定的类,例如需要扩展HibernateDaoSupport.

For example if we do not use the annotation based approach our dao will directly depend of spring specific classes like need to extend the HibernateDaoSupport.

但是,即使有了注释,DAO仍然依赖于Spring知道吗?因为@Repository是spring注释.我们不能完全独立于春天,知道吗?它更像是依赖于spring注释而不是依赖于spring类,对吗?

But even with the annotations still the DAO is depend on Spring know? because @Repository is spring annotation. We cant have a total independency with spring know? Its more like depending on a spring annotation is rather good than depending on a spring class, is it?

一段时间后,我只是想好了,我们需要用别的东西换弹簧.在这种情况下,如果我们的DAO对spring的依赖度为零,那么我们根本就不需要接触我们的DAO.

I was just thinking ok some time later we need to switch spring with some thing else. In that case if our DAO's has zero dependency with spring, we do not need to touch our DAO's at all know.

推荐答案

要实现完全的去耦,就必须像已经发现的那样远离注释.要么必须使用基于Spring XML的配置,要么创建

To achieve complete decoupling, you will have to move away from annotations, just as you already discovered. Either, you'll have to use the spring XML-based configuration, or you create a @Configuration class (aka java-based configuration) that builds up your bean factory.

我只想对您的想法发表评论.花时间在完全去耦的解决方案上,原因是也许将来某个时候我们会想要切换",这听起来像是在浪费我很多时间.您是否有任何理由怀疑或假设这种转换将在可预见的将来进行,甚至永远进行?很明显,您的去耦成本很高.您必须维护XML文件和/或配置类,而不是易于查看的注释,它们都会变得非常复杂,并且过一会儿便难以概览.

I just want to make a comment on your thinking. Spending time on a completely de-coupled solution with the reason that "maybe sometime in the future we'll want to switch" sounds like a lot of time wasted to me. Do you have any reason to suspect or assume that such a switch will be done in the foreseeable future, or ever? There is a cost to your decoupling which is in clarity. Instead of easy-to-see annotations, you'll have to maintain XML-file(s) and/or configuration classes, which both tend to become pretty complex and hard to overview after a while.

这篇关于在dao中将hibernate与spring集成在一起,而无需依赖spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆