Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Guice injector in its Module?

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?

like image 254
stefan.at.wpf Avatar asked Apr 13 '13 16:04

stefan.at.wpf


People also ask

What does @inject do in Guice?

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.

How do I test a Guice module?

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.

What does Guice module do?

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.

What is @inject annotation in Guice?

@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.


2 Answers

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:

In a @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   } } 

To get a Provider in your configure():

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.

To get an instance in your configure():

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);   } } 
like image 55
Jeff Bowman Avatar answered Sep 24 '22 21:09

Jeff Bowman


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) { ... }  } 
like image 30
Ferran Maylinch Avatar answered Sep 23 '22 21:09

Ferran Maylinch