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

时间:2022-01-03 13:39:59

Edit: not duplicate but almost

编辑:不重复,但差不多

I would like to have my app persistence.xml to be something like

我想让我的应用程序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>

getting these placeholder values from a simple text file somewhere in my source folders.

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

I read about that it's possible when using Spring doing like

我读到了使用Spring做的可能性

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

but here we are not using Spring, just Hibernate and some Primefaces.

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

Is it possible?

可能吗?

Thanks!

谢谢!

Edit: I didn't mention some things, but for reference, I'm also using Shiro Security and Ant to do some stuff. I'll post the solution as an answer. This makes my project have 3 different files with database parameters:

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

  • persistence.xml (Hibernate)
  • persistence.xml(Hibernate)
  • context.xml (Shiro)
  • context.xml(Shiro)
  • database.properties (for Flyway tasks in Ant)
  • database.properties(用于Ant中的Flyway任务)

3 个解决方案

#1


16  

Instead of defining the properties inside persistence.xml you can define them in a standard properties file (key=value) and pass a Properties object to the createEntityManagerFactory() method, e.g.:

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

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

#2


2  

If you are using Maven as the build system, you can use Maven filters to replace the values during build time.

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

Or you can write a simple property placeholder replacement (which is internally used by spring itself)

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

Reference: https://*.com/a/14724719/477435

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

#3


1  

I edited to mention I'm using Shiro Security, that also needs some database parameters. I made it need just 1 database parameters location doing these stay in context.xml and referencing it in the others.

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

1) Ant read context.xml

1)Ant读取context.xml

Context.xml having

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>

did using in Ant build.xml

在Ant build.xml中使用过

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

and then accessing it using

然后使用。访问它

<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 read context.xml

2)persistence.xml读取context.xml

It is possible to use context's datastore using this

可以使用此方法使用上下文的数据存储区

<non-jta-data-source>java:/comp/env/jdbc/postgres</non-jta-data-source>

So, I killed 3 database parameters to just 1.

所以,我将3个数据库参数杀死为1。

Thanks for the help!

谢谢您的帮助!

#1


16  

Instead of defining the properties inside persistence.xml you can define them in a standard properties file (key=value) and pass a Properties object to the createEntityManagerFactory() method, e.g.:

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

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

#2


2  

If you are using Maven as the build system, you can use Maven filters to replace the values during build time.

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

Or you can write a simple property placeholder replacement (which is internally used by spring itself)

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

Reference: https://*.com/a/14724719/477435

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

#3


1  

I edited to mention I'm using Shiro Security, that also needs some database parameters. I made it need just 1 database parameters location doing these stay in context.xml and referencing it in the others.

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

1) Ant read context.xml

1)Ant读取context.xml

Context.xml having

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>

did using in Ant build.xml

在Ant build.xml中使用过

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

and then accessing it using

然后使用。访问它

<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 read context.xml

2)persistence.xml读取context.xml

It is possible to use context's datastore using this

可以使用此方法使用上下文的数据存储区

<non-jta-data-source>java:/comp/env/jdbc/postgres</non-jta-data-source>

So, I killed 3 database parameters to just 1.

所以,我将3个数据库参数杀死为1。

Thanks for the help!

谢谢您的帮助!