如何从Spring获取Hibernate Configuration对象? [英] How can I get the Hibernate Configuration object from Spring?

查看:116
本文介绍了如何从Spring获取Hibernate Configuration对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在非Spring代码中获取Spring定义的Hibernate Configuration和SessionFactory对象.以下是我的applicationContext.xml文件中的定义:

I am trying to obtain Spring-defined Hibernate Configuration and SessionFactory objects in my non-Spring code. The following is the definition in my applicationContext.xml file:

代码:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      <prop key="hibernate.show_sql">true</prop>
      <prop key="hibernate.hbm2ddl.auto">update</prop>
      <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
      <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
    </props>
   </property>
   <property name="dataSource">
    <ref bean="dataSource"/>
   </property>
    </bean>

如果我现在调用getBean("sessionFactory"),我将返回一个$ Proxy0对象,该对象似乎是Hibernate SessionFactory对象的代理.但这不是我想要的-我需要LocalSessionFactoryBean本身,因为我需要访问Configuration和SessionFactory.

If I now call getBean("sessionFactory"), I am returned a $Proxy0 object which appears to be a proxy for the Hibernate SessionFactory object. But that isn't what I want - I need the LocalSessionFactoryBean itself because I need access to the Configuration as well as the SessionFactory.

我需要Configuration对象的原因是我们的框架能够使用Hibernate的动态模型在运行时自动插入映射.这要求我们更改配置并重建SessionFactory.确实,我们要做的就是获取Spring中已经存在的Hibernate配置,以便那些在Spring中已经拥有该信息的客户无需将其复制到hibernate.cfg.xml文件中即可.使用我们的Hibernate功能.

The reason I need the Configuration object is that our framework is able to use Hibernate's dynamic model to automatically insert mappings at runtime; this requires that we change the Configuration and rebuild the SessionFactory. Really, all we're trying to do is obtain the Hibernate config that already exists in Spring so that those of our customers that already have that information in Spring don't need to duplicate it into a hibernate.cfg.xml file in order to use our Hibernate features.

推荐答案

Spring容器的一个晦涩之处是

One obscure feature of the Spring container is the & prefix:

当您需要向容器询问 实际的FactoryBean实例 本身,而不是它产生的豆,你 在"bean ID"前加上&"号 符号&(不带引号)何时 调用 ApplicationContext.所以对于给定 ID为myBeanFactoryBean, 在getBean("myBean")上调用 容器返回的乘积 FactoryBean,然后调用 getBean("&myBean")返回 FactoryBean实例本身.

When you need to ask a container for an actual FactoryBean instance itself, not the bean it produces, you preface the bean id with the ampersand symbol & (without quotes) when calling the getBean method of the ApplicationContext. So for a given FactoryBean with an id of myBean, invoking getBean("myBean") on the container returns the product of the FactoryBean, and invoking getBean("&myBean") returns the FactoryBean instance itself.

因此,在您的情况下,使用getBean("&sessionFactory")应该会返回LocalSessionFactoryBean实例本身.然后,您可以调用.getConfiguration()来获取Configuration对象.

So in your case, using getBean("&sessionFactory") should return you the LocalSessionFactoryBean instance itself. Then you can call .getConfiguration() to get the Configuration object.

这篇关于如何从Spring获取Hibernate Configuration对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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