Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将外部数据插入persistence.xml_Java_Xml_Spring - Fatal编程技术网

Java 将外部数据插入persistence.xml

Java 将外部数据插入persistence.xml,java,xml,spring,Java,Xml,Spring,我想让persistence.xml动态设置它的一些属性,具体来说: <property name="hibernate.connection.password" value="password"/> <property name="hibernate.connection.username" value="username"/> 但这是行不通的。我在这里或谷歌中都没有找到方法,但我已经多次看到了${my.clazz.smth}-way 那么,我如何设置它呢?:) 提前

我想让persistence.xml动态设置它的一些属性,具体来说:

<property name="hibernate.connection.password" value="password"/>
<property name="hibernate.connection.username" value="username"/>
但这是行不通的。我在这里或谷歌中都没有找到方法,但我已经多次看到了${my.clazz.smth}-way

那么,我如何设置它呢?:)


提前谢谢

您引用的值占位符
${my.clazz.smth}
通常从属性文件中读取,而不是直接从类中读取

这是使用Spring的


下面是一个结合了Hibernate和Spring的项目的示例。

如果您确实需要将配置延迟到运行时(例如,从外部源(如Web服务)获取数据库凭据),您可以使用它,特别是对于Hibernate或(v4.X)的旧版本

但请注意,据我所知,无法动态更新
PersentenceUnit
的用户名和密码。每次需要更改其属性时,您都必须从一个新的
配置
实例(一个相当昂贵的操作)构建另一个
EntityManagerFactory
。 无论如何,除非您有很好的理由,否则不要管理应用程序中的数据库凭据,而是将其委托给绑定JNDI的
数据源

Ejb3Configuration cfg = new Ejb3Configuration()
  // Add classes, other properties, etc
  .setProperty("hibernate.connection.password", "xxxx")
  .setProperty("hibernate.connection.username", "yyyy"); 

EntityManagerFactory emf= cfg.buildEntityManagerFactory();

所以,前一段时间解决了这个问题,但我仍然没有回答:

Anthony Accioly为我指出了正确的方向:

我将其添加到applicationContext.xml的entityManagerFactory中

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitPostProcessors">
        <bean class="my.package.SetupDatabase">
        </bean>
    </property>
    //the other stuff
</bean>
这样,在启动整个过程时只需进行一次设置,但所需的数据可能会“外包”


再次感谢你为我指明了正确的方向

如果你正在运行一个Web应用程序,你应该考虑使用JNDI查找。确切地说,我从另一个服务中获得了凭据,但是我只需要在启动服务器时设置它们一次,而不让它们在源上直接可见。我会给你一个建议,如果我找到一个解决方案,我会尝试并发布一个解决方案:)我需要做同样的事情,但没有spring。在我的例子中有什么提示吗?类
LocalContainerEntityManagerFactoryBean
还包含属性
jpapproperties
,它可以实现相同的功能,而不需要
持久化单元后处理器
@DanielArdison来解决您的问题,这可能是相关的。
Ejb3Configuration cfg = new Ejb3Configuration()
  // Add classes, other properties, etc
  .setProperty("hibernate.connection.password", "xxxx")
  .setProperty("hibernate.connection.username", "yyyy"); 

EntityManagerFactory emf= cfg.buildEntityManagerFactory();
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitPostProcessors">
        <bean class="my.package.SetupDatabase">
        </bean>
    </property>
    //the other stuff
</bean>
package my.package;

public class SetupDatabase implements PersistenceUnitPostProcessor {

    private String username;
    private String password;
    private String dbserver;

    public void SetupDatabase(){
        //do stuff to obtain needed information
    }

    public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
        pui.getProperties().setProperty("hibernate.connection.username", username );
        pui.getProperties().setProperty("hibernate.connection.password", password);
        pui.getProperties().setProperty("hibernate.connection.url", dbserver ); 
    }

}