初始化JpaPersistModule的最佳方法 [英] Best way to initialize JpaPersistModule

查看:113
本文介绍了初始化JpaPersistModule的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用com.google.inject.persist.jpa.JpaPersistModule.配置位于persistence.xml文件中,但是某些属性是动态的,我不想将它们存储在此文件中(例如javax.persistence.jdbc.url等),而是从其他来源注入它们.

I use com.google.inject.persist.jpa.JpaPersistModule in my application. The configuration is located in persistence.xml file, but some of the properties are dynamic and I don't want store them in this file (for example javax.persistence.jdbc.url etc) but rather inject them from some other source.

我知道有一个JpaPersistModule.properties(java.util.Properties p)方法可以完全满足我的需要.问题是我看不到将java.util.Properties对象传递给模块的好方法.我不想在模块代码中显式创建java.util.Properties的实例,而是希望使用一些guice风格的机制来注入它.

I know that there's a JpaPersistModule.properties(java.util.Properties p) method that allows to do exactly what I need. The problem is that I don't see a good way to pass that java.util.Properties object to the module. I don't want to explicitely create an instance of java.util.Properties in the module code, but would rather use some guice-style mechanism to inject it.

那有可能吗?您将如何解耦JPA模块及其配置属性?

Is that possible at all? How would you decouple JPA module and its configuration properties?

推荐答案

尝试一下

public class DbModule extends AbstractModule {
private final Properties properties;

public DbModule(Properties properties) {
    this.properties = properties;
}

@Override
protected void configure() {
    JpaPersistModule jpa = new JpaPersistModule("my-unit");
    jpa.properties(properties);
    jpa.configure(binder());
    //bind other stuff here...
}
}

这篇关于初始化JpaPersistModule的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆