如何将外部文件的属性包含到hibernate.cfg.xml中?

J.O*_*sen 3 spring hibernate

我需要能够存储数据库配置属性src|main|java|dbConnection.properties并以jstl表达式的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>
Run Code Online (Sandbox Code Playgroud)

我需要这样的东西:

<?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>
Run Code Online (Sandbox Code Playgroud)

sha*_*zin 6

你可以通过编程方式完成.

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>
Run Code Online (Sandbox Code Playgroud)

dbConnection.properties

connection.driver_class=org.postgresql.Driver
connection.username=postgres
connection.password=postgres
Run Code Online (Sandbox Code Playgroud)

创建时,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();
Run Code Online (Sandbox Code Playgroud)


M. *_*num 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>
Run Code Online (Sandbox Code Playgroud)

免费建议而不是使用内部hibernate连接(不建议在生产中使用)在spring中配置数据源并将其连接到您的 LocalSessionFactoryBean

  • 你需要的一切都在答案中.所以没有hibernate.cfg.xml. (2认同)

Gab*_*uiu 5

下面这个,这个这个,我想出了以下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>
Run Code Online (Sandbox Code Playgroud)

使用此配置,您可以运行validate Maven目标来生成过滤的文件,并查看它们是否正确地重新间隔

如果您使用Maven,这当然很有用.