persistence.xml从.properties文件导入数据库参数值

时间:2013-10-03 23:38:22

标签: java xml hibernate shiro persistence.xml

修改:not duplicate but almost

我想让我的应用程序persistence.xml类似于

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                version="1.0">
   <persistence-unit name="appName" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="${db.dialect'}"/>
            <property name="javax.persistence.jdbc.driver" value="${db.driver}"/>
            <property name="javax.persistence.jdbc.user" value="${db.user}"/>
            <property name="javax.persistence.jdbc.password" value="${db.password}"/>
            <property name="javax.persistence.jdbc.url" value="${db.url}"/>
        </properties>
    </persistence-unit>
</persistence>

从源文件夹中某处的简单文本文件中获取这些占位符值。

我读到使用Spring时可能会这样做

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
</bean>

但是在这里我们不使用Spring,只使用Hibernate和一些Primefaces。

有可能吗?

谢谢!

编辑:我没有提到一些事情,但作为参考,我也在使用Shiro Security和Ant来做一些事情。我将发布解决方案作为答案。这使得我的项目有3个不同的文件,包含数据库参数:

  • persistence.xml(Hibernate)
  • context.xml(Shiro)
  • database.properties(对于Ant中的Flyway任务)

3 个答案:

答案 0 :(得分:22)

您可以在标准属性文件(key = value)中定义它们,并将persistence.xml对象传递给Properties方法,而不是在createEntityManagerFactory()中定义属性,例如:

Properties props = new Properties();
props.load(new FileInputStream("/some/path/persistence.properties"));
EntityManagerFactory factory = Persistence.createEntityManagerFactory("appName", props);

答案 1 :(得分:2)

如果您使用Maven作为构建系统,则可以使用Maven过滤器在构建期间替换值。

或者您可以编写一个简单的属性占位符替换(由spring本身内部使用)

参考:https://stackoverflow.com/a/14724719/477435

答案 2 :(得分:1)

我编辑提到我正在使用Shiro Security,它还需要一些数据库参数。我让它只需要1个数据库参数位置,这些位置保留在context.xml中并在其他参考中引用它。

1)Ant读取context.xml

具有

的Context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Other stuff... -->

    <!-- Shiro's -->
    <Resource name="jdbc/postgres" auth="Container"
        type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://url-to-db/database"
        username="user" password="pass" />
</Context>

在Ant build.xml中使用

<xmlproperty file="/path/to/context.xml" keepRoot="false" semanticAttributes="true" includeSemanticAttribute="true" />

然后使用

访问它
<target name="init-flyway">
    <property name="flyway.url" value="${Resource.url}" />
    <property name="flyway.user" value="${Resource.username}" />
    <property name="flyway.password" value="${Resource.password}" />
    <!-- stuff stuff stuff -->
</target>

2)persistence.xml读取context.xml

可以使用this

来使用上下文的数据存储区
<non-jta-data-source>java:/comp/env/jdbc/postgres</non-jta-data-source>

因此,我将3个数据库参数杀死为1。

感谢您的帮助!