Skip to content

Injector instance

Restrictive options

Guicey itself is compatible with the following guice restrictive options:

public class MyModule extends AbstractModule {
        protected void configure() {

So it is safe to enable them.

Access injector

In some cases it may be important to get injector instance outside of guice context.


Injector is created on dropwizard run phase. Attempt to obtain injector before it will lead to exception.

Injector instance could be resolved with:

  • getInjector() method on GuiceBundle instance (NPE will be thrown if injector not initialized)
  • InjectorLookup.getInjector(app).get() static call using application instance (lookup returns Optional for null safety).

If you need lazy injector reference, you can use InjectorProvider class (it's actually Provider<Injector>):

Provider<Injector> provider = new InjectorProvider(app);
// somewhere after run phase
Injector injector = provider.get();

When you are inside your application class:

public class MyApplication extends Application<Configuration> {

    public void run(TestConfiguration configuration, Environment environment) throws Exception {


Most likely, requirement for injector instance means integration with some third party library. Consider writing custom installer in such cases (it will eliminate need for injector instance).

Inside guice context you can simply inject Injector instance:

@Inject Injector injector;

Injector factory

You can control guice injector creation through ru.vyarus.dropwizard.guice.injector.InjectorFactory.

Default implementation is very simple:

public class DefaultInjectorFactory implements InjectorFactory {

    public Injector createInjector(final Stage stage, final Iterable<? extends Module> modules) {
        return Guice.createInjector(stage, modules);

Injector creation customization may be required by some 3rd party library. For example, netflix governator owns injector creation (see example).

Custom injector factory could be registered in guice bundle builder:

            .injectorFactory(new CustomInjectorFactory())