guice JpaPersistModule with runtime configuration
Asked Answered
O

0

6

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?

Organography answered 14/10, 2015 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.