配置由Guice Module提供的对象 [英] Configure an object provided by a Guice Module
问题描述
我有一个模块
,提供了一个JDBI DBI
实例,如下所示:
I have a Module
that provides a JDBI DBI
instance like this:
@Provides
@Singleton
DBI dbi(DataSource dataSource) { return new DBI(dataSource); }
<类型)。放入JDBI模块本身是不合适的逻辑,因为它对于使用JDBI的任何应用程序而言并不普遍。我有一个钩子来做这种额外的配置?
In another module I want to call some initialization methods on that DBI instance (configuring support for particular data types). It's not appropriate logic to put in the JDBI module itself as it's application specific not general to any application using JDBI. Is there a hook for me to do that kind of "extra" configuration?
我尝试使用 bindListener
方法但是似乎没有以这种方式提供的对象被调用。
I tried using the bindListener
method but it seems like that's not invoked for objects provided in this way.
推荐答案
Guice Injections文档介绍了如何使用@Inject注释方法来调用实例方法。
The Guice Injections documentation describes how to invoke an instance method by annotating the method with @Inject.
没有提到实例可以是Guice模块。因此,您可以这样做:
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.
}
}
这篇关于配置由Guice Module提供的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!