如何交换 hibernate.cfg.xml 属性?
我有一个 Hibernate 配置文件 hibernate.cfg.xml
,其中有硬编码的属性名称,例如:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mysecretpassword</property>
...
</session-factory>
</hibernate-configuration>
我想将用户名和密码等内容交换为.properties
-文件。这样我就会得到以下信息:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">${jdbc.username}</property>
<property name="hibernate.connection.password">${jdbc.password}</property>
...
</session-factory>
</hibernate-configuration>
我该怎么做?对于 Spring 中的数据源,我可以在我的 applicationContext.xml 中使用这个数据源:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
Hibernate 的等效项是什么?
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="hibernate.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 hibernate.cfg.xml 中删除配置参数并声明以下内容:
Remove config parameters from hibernate.cfg.xml and declare the following:
如果这是一个选项,您可以从
hibernate.cfg.xml
中删除用户名和密码,并在放在类路径上的hibernate.properties
文件中声明它们。但您确实需要从 XML 配置文件中删除它们,因为它会覆盖“旧”属性文件中的属性。从文档中:如果这不是一个选项(并且如果您无法在 Spring 配置文件中配置 Hibernate),那么您必须在构建时使用构建工具(Ant、Maven 等)中的一些过滤功能来处理该问题。
If this is an option, you could remove the username and password from the
hibernate.cfg.xml
and declare them in anhibernate.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: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).
Configuration 对象有一个 readProperties 方法(但我不知道你所描述的内容)。如果您计划使用自定义属性或覆盖 hibernate.cfg.xlm,请确保首先调用 configure(),然后调用 setProperty 或 setProperties。
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.