你们的建议是什么?
I found most suitable for me solution - keep injectors and modules in enumeration classes. Advantages:
- injectors and modules created once,
- injectors can be used from different classes while running application (not only at bootstrap),
- injectors kept in one place and can be easily found.
例:
import static ru.package.Modules.*;
public enum Injectors {
FOO_INJECTOR(BarModule.module()),
FOO2_INJECTOR(FOO_INJECTOR.injector(),
Bar2Module.module(), FooModule.module());
private final Injector m_injector;
Injectors (Module... modules) {
m_injector = Guice.createInjector(modules);
}
Injectors (Injector parentInjector, Module... modules) {
m_injector = parentInjector.createChildInjector(modules);
}
public Injector injector() {
return m_injector;
}
}