如何将 hibernate.properties 变量放入 dataSource bean 中?

问题描述 投票:0回答:1

我想将 hibernate.properties 变量放入 dataSource bean (Spring) 属性中。我该怎么做?因为我必须交付我的应用程序,并且它不应该包含不同位置的属性。预先感谢。

hibernate spring-mvc java-ee-6
1个回答
0
投票

首先你会得到一个 Spring 管理的数据源。 第二个你得到一个 Hibernate 管理的数据源,它不能被 Spring 重用。

如果类路径中有 hibernate.properties,Spring LocalSessionFactoryBean 将使用该文件来配置数据库连接、方言和池。或者,您可以在 Spring 中定义一个 DataSource(任何实现 javax.sql.DataSource 的类),并在 LocalSessionFactoryBean(或 AnnotationSessionFactoryBean,如果您使用注释)中显式设置所有 Hibernate 属性。

第一种方法:

加载Hibernate属性:Hibernate属性可以使用Spring的PropertyPlaceholderConfigurer类加载。提供 hibernate.properties 文件位置作为值。如果文件放置在 WEB-INF 目录以外的任何其他目录下,请使用 classpath: 表达式让 Spring 在可用的类路径中查找该文件。

<!– Hibernate Properties –>
  <bean id=”PropertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
     <property name=”locations”>
       <list>
              <value>classpath:/properties/hibernate.properties</value>
       </list>
    </property>
</bean>

第二种方法:http://wiki.apache.org/tapestry/SpringHibernate

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.
annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.xyz.domain.User</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.