Dependency injection 通过GoogleGuice用例进行依赖注入
我的代码看起来像ff:Dependency injection 通过GoogleGuice用例进行依赖注入,dependency-injection,guice,Dependency Injection,Guice,我的代码看起来像ff: Interface i; if (someCondition) { ImplementationA a = new ImplementationA(); a.setFieldA(someValueA); a.setFieldB(someValueB); i = a; } else { ImplementationB b = new ImplementationB(); b.setFieldC(someValueC); i = b; } // D
Interface i;
if (someCondition) {
ImplementationA a = new ImplementationA();
a.setFieldA(someValueA);
a.setFieldB(someValueB);
i = a;
} else {
ImplementationB b = new ImplementationB();
b.setFieldC(someValueC);
i = b;
}
// Do something with i.
我的问题是:
动态运行时注入超出范围。您必须通过模块配置要使用的实现。您仍然可以使用工厂(查看多绑定和辅助注入),省去设置实例的工作,尽管…动态运行时注入超出了范围。您必须通过模块配置要使用的实现。您仍然可以使用工厂(查看多绑定和辅助注入),并节省设置实例的工作