1

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?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
andrew.z
  • 1,039
  • 3
  • 15
  • 24

2 Answers2

0

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...
}
}
julius
  • 815
  • 2
  • 7
  • 12
  • 1
    you should use `install(new JpaPersistModule("my-unit").properties(properties))`. From documentation on configure method `Do not invoke this method directly to install submodules` – Vincnetas Feb 27 '17 at 14:01
0

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.

ColinD
  • 108,630
  • 30
  • 201
  • 202