java - 具有间接依赖性的 Guice 中的多个实现

标签 java dependency-injection guice

我的 Java 代码使用一个接口(interface)的多个实现(下例中的 BugTemplate),其中绑定(bind)的实现取决于上下文。

在 Guice 中,我相信完成此任务的正确方法是使用 BindingAnnotations。但是,我的用例与规范示例不同,因为上下文是从接口(interface)实现中删除的一个级别。

总结可能的依赖关系:

FooBugFinder -> BugFiler -> FooBugTemplate

BarBugFinder -> BugFiler -> BarBugTemplate

etc.

示例代码:

class FooBugFinder {
  // ...
  public findBugsAndFileThem() {
    List<Bug> bugs = findBugs();
    bugFiler.fileBugs(bugs);
  }
  @Inject
  FooBugFinder(BugFiler bugFiler) {
    // BugFiler should have been instantiated with a FooBugTemplate.
    this.bugFiler = bugFiler;
  }
}

class BugFiler {
  // ...
  public fileBugs(List<Bug> bugs) {
    List<FormattedBugReport> bugReports = bugTemplate.formatBugs(bugs);
    fileBugReports(bugReports);
  }
  @Inject
  BugFiler(BugTemplate bugTemplate) {
    // The specific implementation of BugTemplate is context-dependent.
    this.bugTemplate = bugTemplate;
  }
}

interface BugTemplate {
  List<FormattedBugReport> formatBugs(List<Bug> bugs);
}

class FooBugTemplate implements BugTemplate {
  @Overrides
  List<FormattedBugReport> formatBugs(List<Bug> bugs) {
    // ...
  }
}

我的第一个想法是按如下方式注释ctor:

FooBugFinder(@Foo BugFiler bugFiler) { }

但是,Guice 如何知道在向 BugFiler 的构造函数注入(inject)参数时应用该注释?

换句话说,FooBugFinder 需要一个用 FooBugTemplate 实例化的 BugFiler 实例。 BarBugFinder 需要一个用 BarBugTemplate 实例化的 BugFiler 实例。

有什么想法吗?

最佳答案

您可以通过创建一个公开带注释的 BugFiler 对象的私有(private)模块来实现:

abstract class BugFilerModule extends PrivateModule {
  private final Class<? extends Annotation> annotation;

  BugFilerModule(Class<? extends Annotation> annotation) {
    this.annotation = annotation;
  }

  @Override protected void configure() {
    bind(BugFiler.class).annotatedWith(annotation).to(BugFiler.class);
    expose(BugFiler.class).annotatedWith(annotation);
    bindBugTemplate();
  }

  abstract void bindBugTemplate();
}

然后,当您创建注入(inject)器时:

    Injector injector = Guice.createInjector(
        new BugFilerModule(Foo.class) {
          @Override void bindBugTemplate() {
            bind(BugTemplate.class).to(FooBugTemplate.class);
          }
        },
        new BugFilerModule(Bar.class) {
          @Override void bindBugTemplate() {
            bind(BugTemplate.class).to(BarBugTemplate.class);
          }
        },
        /* other modules */);

然后您可以按照您建议的方式创建一个FooBugFinder:

public class FooBugFinder {
  @Inject
  public FooBugFinder(@Foo BugFiler fooBugFiler) {
    ...
  }
}

关于java - 具有间接依赖性的 Guice 中的多个实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372284/

相关文章:

java - ehCache缓存有自动刷新的选项吗?没有任何调度程序工作?

jsf - JSF2.0中如何注入(inject)ManagedBean

java - 什么是 Guice Mapbinder 的好用例?

java - 使用 GUICE 注入(inject) HttpClient 以在 Java 中获取模拟响应

java - 在 Java 中的数字类上使用 new 和构造函数有什么合理的理由吗?

java - CouchDB Java 客户端

java - 打印字符串中 Unicode 最低的字符

java - guice绑定(bind)到实例和asEagersingleton有什么区别

.net - StructureMap 中的 AddRegistry 和 IncludeRegistry 有什么区别?

java - 绑定(bind)到 TypeLiteral 是 google guice 中的好习惯还是坏习惯