2011-12-14 155 views
0

我在我的应用程序中使用了com.google.inject.persist.jpa.JpaPersistModule。该配置位于persistence.xml文件中,但其中一些属性是动态的,我不希望将它们存储在此文件中(例如javax.persistence.jdbc.url等),而是从其他来源注入它们。初始化JpaPersistModule的最佳方法

我知道有一个JpaPersistModule.properties(java.util.Properties p)方法可以完成我所需要的。问题是,我没有看到将该对象传递给模块的好方法。我不想在模块代码中明确创建一个java.util.Properties的实例,但宁愿使用一些guice风格的机制来注入它。

这可能吗?你将如何分解JPA模块及其配置属性?

回答

0

Module通常手动创建,因为它们是在Injector之前创建的。如果你真的需要真实,例如创建一个注入器,使用它创建一个或多个模块,然后使用这些模块创建子注入器,则可以跳过一些注入模块以注入模块。但我并没有真正看到这一点。手动创建Properties并将它们传递给我看起来不是什么大问题。

0

试试这个

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... 
} 
} 
+0

你应该使用`安装(新JpaPersistModule( “我的单位”)。属性(Properties))`。从配置方法的文档`不要直接调用这个方法来安装子模块` – Vincnetas 2017-02-27 14:01:21

相关问题