如何让hibernate不通过spring配置读取类路径中的“hibernate.properties”

时间:2011-12-13 10:10:16

标签: hibernate spring configuration

classpath中有一个“hibernate.properties”文件,由某些工具共享,所以我无法将其删除。

但是我想创建一个用于测试的“hibernate.test.properties”,其内容与“hibernate.properties”不同。

然后我配置弹簧:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.test" />

    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:hibernate.test.properties</value>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${hibernate.connection.driver_class}" />
        <property name="url" value="${hibernate.connection.url}" />
        <property name="username" value="${hibernate.connection.username}" />
        <property name="password" value="${hibernate.connection.password}" />
    </bean>

    <bean id="hibernateProps"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.c3p0.minPoolSize">${hibernate.c3p0.minPoolSize}</prop>
                <prop key="hibernate.c3p0.maxPoolSize">${hibernate.c3p0.maxPoolSize}</prop>
                <prop key="hibernate.c3p0.idleTestPeriod">${hibernate.c3p0.idleTestPeriod}s</prop>
                <prop key="hibernate.c3p0.timeout">${hibernate.c3p0.timeout}</prop>
                <prop key="hibernate.c3p0.max_statement">${hibernate.c3p0.max_statement}</prop>
                <prop key="hibernate.c3p0.testConnectionOnCheckout">${hibernate.c3p0.testConnectionOnCheckout}</prop>
                <prop key="hibernate.c3p0.preferredTestQuery">${hibernate.c3p0.preferredTestQuery}</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties" ref="hibernateProps" />
        <property name="packagesToScan" value="com.test.pojo" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <constructor-arg ref="sessionFactory" />
    </bean>

</beans>

但我发现虽然我在这个文件中指定了新属性,但hibernate仍会从“hibernate.properties”中读取属性并覆盖它们。所以我总是得错了。

有没有办法解决它?

2 个答案:

答案 0 :(得分:2)

我可以提供几种选择。

  1. 在hibernate.test.properties中,尝试更改属性名称。
  2. 将单独的属性文件注入hibernateproperties

    <property name="hibernateProperties">
           <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean.PropertiesFactoryBean">
           <property name="location" value="classpath:myHibernate.properties"/>
       </bean>
    </property>
    

答案 1 :(得分:1)

使用Spring的新版本(3或3.1?),您可以使用Spring配置文件:

<beans profile="test">
   ... your bean definitions
</beans>
<beans profile="normal">
   ... your bean definitions
</beans>

然后你可以使用Aravind A方法

在“旧方式”中,您可以分离正常和测试弹簧上下文。这可能就是你所做的,但是你的常规hibernate.properties是如何加载并用作占位符的呢?您的上下文层次结构中可能存在一些问题,因为您所做的事情应该起作用