Dependency injection 通过GoogleGuice用例进行依赖注入

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

我的代码看起来像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;
}
// Do something with i.
我的问题是:

  • 我应该在这里使用依赖注入,还是这超出了技术的范围
  • 如果我应该在这里使用依赖注入,我如何使用GoogleGuice实现它

  • 动态运行时注入超出范围。您必须通过模块配置要使用的实现。您仍然可以使用工厂(查看多绑定和辅助注入),省去设置实例的工作,尽管…

    动态运行时注入超出了范围。您必须通过模块配置要使用的实现。您仍然可以使用工厂(查看多绑定和辅助注入),并节省设置实例的工作