Injecting into static field in Guice

Tags:

To handle static field injection, call requestStaticInjection.

@ImplementedBy(ManagerImpl.class) // replaced by ManagerImpl automatically without bind(..)
interface Manager { }

class ManagerImpl implements Manager { }

class MyModule implements Module {
  public void configure(Binder binder) {
    binder.requestStaticInjection(App.class); 
  }
}

public class App {
  @Inject
  private static Manager manager;
  public static void main(String[] args) {
    Guice.createInjector(new Module[] { new MyModule() });
    manager.sayHi();
  }
}