2011-12-13 20 views
1

讀取classpath中「hibernate.properties」有在由一些工具共享類路徑「hibernate.properties」文件,所以無法將其刪除。如何讓Hibernate不要通過Spring配置

但我想創建一個測試,其內容是從「hibernate.properties」不同「hibernate.test.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

我可以給幾個選項。

  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> 
    
+0

兩個工作!我希望我能給你10票! – Freewind

+0

使用'org.springframework.beans.factory.config.PropertiesFactoryBean',而不是'org.springframework.beans.factory.config.PropertiesFactoryBean.PropertiesFactoryBean'(春季3.1.X) – dtrunk

1

隨着春天的新版本,你可以使用Spring配置文件(3或3.1):

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

然後你可以使用亞拉文一方法

在「老辦法」,你可以分離的正常和測試Spring上下文的。這也許是你做了什麼,但你的正常hibernate.properties是如何加載並用作佔位符?有可能是你的上下文結構一些問題,因爲你做了什麼應該工作

+0

+1爲Spring配置文件的事情。 –

相關問題