I am extending Guice's AbstractModule
and inside of the extending class I need access to Guice's injector. It that possible, if yes, how?
This is an unusual request. Modules are more like config files than logic files: The Module is read to create the Injector, and then as soon as the Injector is created the module has done its job. For a simple Module, the Injector literally doesn't exist until the Module is ready to be discarded.
In any case, rather than requesting an Injector to get class X, you should typically request a Provider<X>
. Guice will inject an X
or Provider<X>
for any binding of X
, Provider<X>
, or @Provides X
, so you can almost always do this instead. That said, injecting the Injector will allow you to get an instance reflectively, or to inspect the Injector's bindings (etc).
Here are a few valid reasons/designs that would require accessing an Injector from within a Module:
@Provides
method:Modules can contain mini-providers in methods annotated with @Provides
. Remember that Injector
is injectable: If you need an Injector in one of those methods, you can just accept it as a parameter:
public class OneModule extends AbstractModule {
@Override public void configure() { /* ... */ }
@Provides YourDependency getYourDependency(Injector injector) {
return injector.getInstance(Class.forName(yourDependencyName));
}
@Provides Something getSomething(SomethingImpl something) {
return initialize(something); // preferred: only ask for what you need
}
@Provides SomethingElse getSomethingElse(Provider<Thing> thingProvider) {
return new SomethingElse(thingProvider); // asking for a provider works too
}
}
AbstractModules expose getProvider()
for exactly this reason, though you'll get an error if you call get()
on that Provider before the injector is ready to provide it (such as at configuration time):
public class TwoModule extends AbstractModule {
@Override public void configure() {
bind(Thingy.class).toInstance(
new MyThingy(8675309, getProvider(Another.class)));
}
}
You can probably call getProvider(Injector.class)
but I don't know whether that works and I don't know why you'd want to.
This is a bad idea; Guice is not ready to provide instances until after all of the configure methods run. The closest you can get is to create a child Injector using the other modules and pass it into this module, but even that is rarely needed.
public class MainClass {
public static void main(String[] args) {
Injector firstStage =
Guice.createInjector(new OtherModule1(), new OtherModule2());
// An alternative design would @Inject-annotate fields in ThreeModule
// and get it from firstStage, but that's nonstandard and may be confusing.
Injector secondStage =
firstStage.createChildInjector(new ThreeModule(firstStage));
}
}
public class ThreeModule extends AbstractModule {
private final Injector otherInjector;
public ThreeModule(Injector otherInjector) {
this.otherInjector = otherInjector;
}
@Override public void configure() {
bindStuffBasedOn(otherInjector);
}
}