Best way to initialize JpaPersistModule

风格不统一 提交于 2019-12-08 11:20:28

问题


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.

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.

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


回答1:


Modules are generally created manually, because they're created before the Injector is. You can jump through some hoops to inject a module if you really want, such as creating one injector, using it to create one or more modules, and then creating a child injector using those. I don't really see the point though. Creating the Properties manually and passing them in doesn't seem like a big deal to me.




回答2:


Try this

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


来源:https://stackoverflow.com/questions/8502271/best-way-to-initialize-jpapersistmodule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!