0
votes

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?

2

2 Answers

0
votes

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.

0
votes

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