Google guice に static 宣言の Object にインジェクションする方法はあるのか?
⇒ ある。
Module の configure() メソッド実行の中で、requestStaticInjection(Class<?> ) で対象クラスを指定すればよい。
@Override public void configure() { requestStaticInjection(ProcessorFactory.class); ... }
class ProcessorFactory { @Inject static Provider<Processor> processorProvider; /** * @deprecated prefer to inject your processor instead. */ @Deprecated public static Processor getInstance() { return processorProvider.get(); } }
でも、
Injections · google/guice Wiki · GitHub
には、こう書いてあるように、使わない方が良いのだろうな。
Static members will not be injected at instance-injection time.
This API is not recommended for general use because it suffers many of the same problems as static factories:
it's clumsy to test, it makes dependencies opaque, and it relies on global state.