2014-06-12 75 views
2

我需要能夠在src|main|java|dbConnection.properties中存儲數據庫配置屬性,並以jstl表達式的形式將它包含到hibernate.cfg.xml。 (如:$ {密碼}等)。怎麼做?如何將外部文件的屬性包含到hibernate.cfg.xml中?

當前的hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
<property name="connection.driver_class">org.postgresql.Driver</property> 
     <property name="connection.username">postgres</property> 
     <property name="connection.password">postgres</property> 
     <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 
    </session-factory> 
</hibernate-configuration> 

我需要的是這樣的:

<?xml version='1.0' encoding='utf-8'?> 
    <!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD//EN" 
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
     <session-factory> 
    <property name="connection.driver_class">${DRIVER}</property> 
      <property name="connection.username">${USERNAME}</property> 
      <property name="connection.password">${PASSWORD}</property> 
      <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 
     </session-factory> 
    </hibernate-configuration> 
+1

如果您在使用Spring爲什麼連打擾一個hibernate.cfg.xml。在春季配置'SessionFactory'並讓Spring爲您替換佔位符。 –

回答

6

你說你使用Spring,那麼爲什麼不讓Spring做所有的努力。讓一個屬性佔位符替換你想要的佔位符。

<context:property-placeholder location="classpath:dbConnection.properties" /> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="hibernateProperties"> 
     <map> 
      <entry key="connection.driver_class" value="${DRIVER}" /> 
      <entry key="connection.username" value="${USERNAME}" /> 
      <entry key="connection.password" value="${PASSWORD}" /> 
      <entry key="transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" /> 
     </map> 
    <property> 
</bean> 

免費諮詢,而不是使用Hibernate內部連接東西(這是不勸在生產中使用)在春季和線配置一個數據源,爲您的LocalSessionFactoryBean

+0

我是Hibernate的新手。你可以給這個設置所需的所有配置示例嗎?應該在'hibernate.cfg.xml'中留下什麼配置? –

+2

你需要的一切都在答案中。所以沒有hibernate.cfg.xml。 –

+0

一定要使用合適的包,如hibernate4.LocalSessionFactoryBean,它與您的hibernate版本相匹配。還包括spring-orm依賴。 –

5

你可以做到這一點編程。

hibernate.cfg.xml應該如下。

<?xml version='1.0' encoding='utf-8'?> 
    <!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD//EN" 
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
     <session-factory> 
      <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 
     </session-factory> 
    </hibernate-configuration> 

dbConnection.properties

connection.driver_class=org.postgresql.Driver 
connection.username=postgres 
connection.password=postgres 

和創建SessionFactory,你可以做的時候以下。

Properties dbConnectionProperties = new Properties(); 
try { 
    dbConnectionProperties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("dbConnection.properties")); 
} catch(Exception e) { 
    // Log 
} 

SessionFactory sessionFactory = new Configuration().mergeProperties(dbConnectionProperties).configure().buildSessionFactory(); 
+0

dbConnectionProperties的'load'方法不能是'String'類型。 IDE建議:void load(Reader)或void load(InputStream)。如何解決它? –

+0

請參閱編輯 – shazin

+0

謝謝!最近發現了另一種做法。爲什麼你的解決方案比這更好:http://stackoverflow.com/questions/17939339/propertyplaceholderconfigurer-with-hibernate-cfg-xml? –

3

thisthisthis我想出了以下Maven配置替換/過濾器​​從你的hibernate.cfg.xml文件與從屬性的屬性佔位符文件:

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-resources-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 

       <execution> 
        <id>copy-resources</id> 
        <!-- here the phase you need --> 
        <phase>validate</phase> 
        <goals> 
         <goal>copy-resources</goal> 
        </goals> 
        <configuration> 
         <outputDirectory>${basedir}/target/extra-resources</outputDirectory> 
         <resources> 
          <resource> 
           <directory>src/main/resources</directory> 
           <filtering>true</filtering> 
          </resource> 
         </resources> 
        </configuration> 
       </execution> 
      </executions> 

     </plugin> 
    </plugins> 

    <!-- Specify the file that contains the value to replace the placeholders --> 
    <filters> 
     <filter>src/main/resources/dbConnection.properties</filter> 
    </filters> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <filtering>true</filtering> 
      <excludes> 
       <exclude>*</exclude> 
      </excludes> 
      <includes> 
       <include>hibernate.cfg.xml</include> 
      </includes> 
     </resource> 
    </resources> 
</build> 

有了這個配置,您可以運行驗證 Maven目標來生成過濾的文件並查看它們是否被正確地重新綁定

如果您使用Maven,這當然很有用。

相關問題