简体   繁体   English

在没有spring依赖的情况下将hibernate与spring集成在一起

[英]integrate hibernate with spring without spring dependency in dao

When we integrate hibernate with spring we normally implement annotation based approach use by @Repository spring annotation. 当我们将hibernate与spring集成时,我们通常会使用@Repository spring注释实现基于注释的方法。 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 我了解到它的目的是为了消除我们的dao中的spring依赖性,因为hibernate3支持上下文会话来管理会话

@Repository
public class HibernateSpitterDao implements SpitterDao{

privateSessionFactorysessionFactory;

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

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

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特定的类,例如需要扩展HibernateDaoSupport。

But even with the annotations still the DAO is depend on Spring know? 但即使有注释仍然DAO依赖于Spring知道吗? because @Repository is spring annotation. 因为@Repository是spring注释。 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? 它更像是依赖于弹簧注释比依赖于弹簧类更好,是吗?

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. 在这种情况下,如果我们的DAO与spring没有任何依赖关系,我们根本不需要触摸我们的DAO。

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. 要么,您必须使用基于Spring的Spring配置,要么创建一个构建bean工厂的@Configuration类(也就是基于java的配置)。

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. 您需要维护XML文件和/或配置类,而不是易于查看的注释,这些类文件和/或配置类在一段时间后都会变得相当复杂且难以概述。

I would say that the example you provided is not tightly dependent on Spring. 我会说你提供的例子并不是严格依赖于Spring。 The only Spring component you are using is the @Repository annotation, which 1) acts as a @Component and let's you component scan the class, and 2) makes your class eligible for data access translation ( Source ). 您正在使用的唯一Spring组件是@Repository注释,其中1)充当@Component并让您组件扫描该类,并且2)使您的类符合数据访问转换的条件( )。

That's not coupling your implementation to Spring at all. 这根本不是你的实现与Spring的结合。 Instead, you are using Hibernate's Session Factory and you are coupled to that. 相反,你正在使用Hibernate的会话工厂,你就是这样。 That's okay that your implementation is coupled with Hibernate. 你的实现与Hibernate相结合是可以的。

In fact, that's the entire purpose of defining an interface and having different implementations of it. 实际上,这是定义接口并具有不同实现的完整目的。 In this case, you have a SpitterDAO interface and a HibernateSpitterDAO implementation. 在这种情况下,您有一个SpitterDAO接口和一个HibernateSpitterDAO实现。 If you decide in the future to utilize iBatis or Spring's HibernateTemplate or JDBCTemplate, you could write a different implementation. 如果您决定将来使用iBatis或Spring的HibernateTemplate或JDBCTemplate,您可以编写不同的实现。

Spring tries to stay out of your way for the most part. 春天试图在大多数情况下避开你的方式。 It's largely used as a container to manage dependency injection and really just coordinates how your code is "glued together". 它主要用作管理依赖注入的容器,实际上只是协调代码如何“粘合在一起”。 I think code gets tightly coupled with Spring when you have classes that implements things like ApplicationSessionAware and BeanPostProcessor, which I would try to avoid as much as possible. 我认为当你有类实现ApplicationSessionAware和BeanPostProcessor之类的类时,代码会与Spring紧密结合,我会尽量避免使用它。

And just because I wasn't sure if you knew the pros/cons between SessionFactory and Spring's HibernateTemplate, here is a great blog post from SpringSource comparing and constrasting them: http://blog.springsource.org/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/ 只是因为我不确定你是否知道SessionFactory和Spring的HibernateTemplate之间的优缺点,这里有一篇来自SpringSource的博客文章比较和对比它们: http//blog.springsource.org/2007/06/26/所以,应该任您还在使用,弹簧-的HibernateTemplate,安道尔,使用JpaTemplate /

Here is a snippet of final suggestion from the blog post: 以下是博文中的最终建议片段:

So in short (as the JavaDoc for HibernateTemplate and JpaTemplate already mention) I'd recommend you to start using the Session and/or EntityManager API directly if you're starting to use Hibernate or JPA respectively on a new project–remember: Spring tries to be non-invasive, this is another great example! 所以简而言之(就像已经提到的HibernateTemplate和JpaTemplate的JavaDoc一样)我建议你直接开始使用Session和/或EntityManager API,如果你开始分别在一个新项目上使用Hibernate或JPA - 记住:Spring尝试要是非侵入性的,这是另一个很好的例子!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM