将外部依赖的构造函数传递到Guice实现中

问题描述

我有一个作业,应该从深度存储中读取数据。我正在为我的项目使用Guice DI。

已经编写了一个深层存储,并将其作为外部依赖。我正在努力在Guice中实例化客户端

这是代码

JobModule

public class JobModule extends AbstractModule {
  private Config config;

  JobModule(Config config) {
     this.config = config;
  }

  @Override
  protected void configure() {
    bind(Reader.class).to(DeepStoreReader.class);
  }

  @Provides
  @Named("config")
  Config provideConfig() {
    return this.config;
  }
}

阅读器界面

public interface Reader {
  List<String> getData(String path);
}

DeepStoreReader

public class DeepStoreReader implements Reader {
  private final DeepStoreClient deepStoreClient;

  DeepStoreReader(@Named("config") Config config) {
     this.deepStoreClient = new DeepStoreClient(config);
  }

  @Override
  public List<String> getData(String path) {
    return this.deepStoreClient.getData(path);
  }
}

问题是我不想在DeepStoreClient构造函数中实例化DeepStoreReader,因为很难测试DeepStoreReader,因为我无法模拟{{ 1}}

在这种情况下实例化客户端的首选方法是什么? DeepStoreClient不是Guice模块/实现,它是外部发布的依赖项

PS:我是DI的新手,正在学习Guice

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)