【问题标题】:Dependency injection via Google Guice use case通过 Google Guice 用例进行依赖注入
【发布时间】:2012-07-20 01:49:17
【问题描述】:

我的代码看起来像 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.

我的问题是:

  1. 我应该在这里使用依赖注入还是超出了该技术的范围?
  2. 如果我应该在这里使用依赖注入,我该如何使用 Google Guice 来完成它?

【问题讨论】:

    标签: dependency-injection guice


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2015-12-06
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      相关资源
      最近更新 更多