Accessing Guice injector in its Module?

前端 未结 2 631
日久生厌
日久生厌 2020-12-01 04:26

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?

2条回答
  •  独厮守ぢ
    2020-12-01 05:03

    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) { ... }
    
    }
    

提交回复
热议问题