How to swap out hibernate.cfg.xml properties?
I've got a Hibernate configuration file hibernate.cfg.xml
where are hard-coded property names like:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysecretpassword</property>
...
</session-factory>
</hibernate-configuration>
I want to swap out things like the username and the password to a .properties
-file. So that I will get the following:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">${jdbc.username}</property>
<property name="hib开发者_如何学Pythonernate.connection.password">${jdbc.password}</property>
...
</session-factory>
</hibernate-configuration>
How can I do that? For the dataSource in Spring I can use this one in my applicationContext.xml
:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
What is the equivalent for Hibernate?
Remove config parameters from hibernate.cfg.xml and declare the following:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
</value>
</property>
</bean>
If this is an option, you could remove the username and password from the hibernate.cfg.xml
and declare them in an hibernate.properties
file that you put on the classpath. But you really need to remove them from the XML configuration file as it overrides properties from the "legacy" properties file. From the documentation:
3.7. XML configuration file
An alternative approach to configuration is to specify a full configuration in a file named
hibernate.cfg.xml
. This file can be used as a replacement for thehibernate.properties
file or, if both are present, to override properties.
If this is not an option (and if you can't configure Hibernate in your Spring configuration file), then you'll have to handle that at build time, using some filtering features from your build tool (Ant, Maven, etc).
a Configuration object has a readProperties method (I don't know about what you described though). If you do plan on using custom properties or overriding hibernate.cfg.xlm, make sure to call configure() first, then setProperty or setProperties after it.
精彩评论