Configure an object provided by a Guice Module

前端 未结 2 620
一向
一向 2021-01-28 18:00

I have a Module that provides a JDBI DBI instance like this:

@Provides
@Singleton
DBI dbi(DataSource dataSource) { return new DBI(dataS         


        
2条回答
  •  臣服心动
    2021-01-28 18:38

    The Guice Injections documentation describes how to invoke an instance method by annotating the method with @Inject.

    It doesn't mention that the instance can be a Guice module. As such, you can do this:

    class MyConfigurationModule extends AbstractModule {
      @Override
      protected void configure() {
        requestInjection(this);
      }
    
      @Inject
      void configureDbi(DBI dbi) {
        // Do whatever configuration.
      }  
    }
    

提交回复
热议问题