小编pau*_*chf的帖子

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

编辑:不重复但差不多

我想让我的应用程序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>
Run Code Online (Sandbox Code Playgroud)

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

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

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:com/foo/jdbc.properties</value>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

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

可能吗?

谢谢!

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

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

java xml hibernate persistence.xml shiro

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

hibernate ×1

java ×1

persistence.xml ×1

shiro ×1

xml ×1