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?
Note that the only Guice-specific code in the above is the @Inject annotation. This annotation marks an injection point. Guice will attempt to reconcile the dependencies implied by the annotated constructor, method, or field.
Typically the best way to test Guice modules is to just create an injector in your test and ensure you can get instances of keys you care about out of it. To do this without causing production stuff to happen you may need to replace some modules with other modules. You can use Modules.
The Guice module helps you to inject Guice managed components into your play application. The injection points are defined by the upcoming @javax. inject. Inject annotation, which is bundled with play at the moment.
@Target(value={METHOD,CONSTRUCTOR,FIELD}) @Retention(value=RUNTIME) @Documented public @interface Inject. Annotates members of your implementation class (constructors, methods and fields) into which the Injector should inject values. The Injector fulfills injection requests for: Every instance it constructs.
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); } }
You can inject the Injector
in your class or provider, but it should be used sparsely.
I found it here: https://groups.google.com/d/msg/google-guice/EiMDuDGei1Q/glxFhHKHHjsJ
See also: https://github.com/google/guice/wiki/InjectingTheInjector
public class MyClass { @Inject public MyClass(Injector injector) { ... } } public class MyModule extends AbstractModule { ... @Provides public Something provideSomething(Injector injector) { ... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With