4

I need to share datasource with JpaPersistModule. This datasource is provided by guice injector.

Now the problem I have to build module during configuration phase, but datasource is available only in runtime.

Currently I have following code:

public class MyJpaConfigurationModule implements Module {

   private Map<String, Object> jpaProperties = new HashMap<>();
   private Module jpaModule = new JpaPersistModule("peristenceUnit").properties(jpaProperties);

   public void configure(Binder binder) {
      binder.requestInjection(this);
      binder.install(jpaModule);
   }

   @Provides @Singleton
   public DataSource provideDatasource() {
       return ..... // some data source
   }

   @Inject
   public void setJpaProperties(DataSource dataSource, PersistService persistService) {
      jpaProperties.put("dataSource", dataSource);

      persistService.start();
   }
}

I have checked and it seems that jpa properties map is everywhere injected by reference, so my runtime changes should become visible, but what if this changes in future?

What is the correct way to resolve such conflicts?

michael nesterenko
  • 13,240
  • 23
  • 104
  • 175

0 Answers0