2014-06-12 55 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,这当然很有用。

相关问题