配置Guice模块提供的对象

Rob*_*her 1 java dependency-injection guice

我有一个Module提供这样的JDBI DBI实例:

@Provides
@Singleton
DBI dbi(DataSource dataSource) { return new DBI(dataSource); }
Run Code Online (Sandbox Code Playgroud)

在另一个模块中,我想在该DBI实例上调用一些初始化方法(配置对特定数据类型的支持).放入JDBI模块本身并不合适,因为它的应用程序特定于使用JDBI的任何应用程序都不通用.有没有勾到我做那种"额外"的配置?

我尝试使用该bindListener方法,但似乎没有调用以这种方式提供的对象.

And*_*ner 8

吉斯注射文档描述了如何通过注释与@注入的方法来调用一个实例方法.

它没有提到实例可以是Guice模块.因此,您可以这样做:

class MyConfigurationModule extends AbstractModule {
  @Override
  protected void configure() {
    requestInjection(this);
  }

  @Inject
  void configureDbi(DBI dbi) {
    // Do whatever configuration.
  }  
}
Run Code Online (Sandbox Code Playgroud)