如何询问hibernate don';不要读那句话;hibernate.properties“;通过spring配置在类路径中

如何询问hibernate don';不要读那句话;hibernate.properties“;通过spring配置在类路径中,hibernate,spring,configuration,Hibernate,Spring,Configuration,类路径中有一个由一些工具共享的“hibernate.properties”文件,所以我无法删除它 但是我想为测试创建一个“hibernate.test.properties”,它的内容不同于“hibernate.properties” 然后配置弹簧: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="ht

类路径中有一个由一些工具共享的“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.test.properties
${hibernate.hbm2ddl.auto}
${hibernate.dial}
${hibernate.show_sql}
${hibernate.c3p0.minPoolSize}
${hibernate.c3p0.maxPoolSize}
${hibernate.c3p0.idleTestPeriod}s
${hibernate.c3p0.timeout}
${hibernate.c3p0.max_语句}
${hibernate.c3p0.testConnectionOnCheckout}
${hibernate.c3p0.preferredTestQuery}
但是我发现,尽管我在这个文件中指定了新属性,hibernate仍然会从“hibernate.properties”中读取属性并覆盖它们。所以我总是接错电话


有没有办法解决这个问题?

我可以给你几个选择

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

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

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

    <beans profile="test">
       ... your bean definitions
    </beans>
    <beans profile="normal">
       ... your bean definitions
    </beans>
    
    
    ... 你的bean定义
    ... 你的bean定义
    
    然后你可以用Aravind A方法


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

    使用
    org.springframework.beans.factory.config.PropertiesFactoryBean
    而不是
    org.springframework.beans.factory.config.PropertiesFactoryBean.PropertiesFactoryBean
    (spring 3.1.x)