如何交换 hibernate.cfg.xml 属性?

发布于 2024-09-24 18:44:49 字数 1103 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有一腔孤勇 2024-10-01 18:44:49

从 hibernate.cfg.xml 中删除配置参数并声明以下内容:

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

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>
浴红衣 2024-10-01 18:44:49

如果这是一个选项,您可以从 hibernate.cfg.xml 中删除用户名和密码,并在放在类路径上的 hibernate.properties 文件中声明它们。但您确实需要从 XML 配置文件中删除它们,因为它会覆盖“旧”属性文件中的属性。从文档中:

3.7。 XML配置文件

另一种方法
配置是指定一个完整的
配置在一个名为
hibernate.cfg.xml。该文件可以是
用作替代品
hibernate.properties 文件,或者,如果两者都有
存在,以覆盖属性。

如果这不是一个选项(并且如果您无法在 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 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 the
hibernate.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).

小…楫夜泊 2024-10-01 18:44:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文