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

Java 将外部依赖项的构造函数传递到Guice实现中,java,dependency-injection,guice,factory,Java,Dependency Injection,Guice,Factory,我有一份工作,应该从深层存储器中读取数据。我正在为我的项目使用guicedi 有一个深层的存储已经编写和未来作为一个外部依赖。我正在努力用Guice实例化客户机 这是密码 作业模块 public class JobModule extends AbstractModule { private Config config; JobModule(Config config) { this.config = config; } @Override protected

我有一份工作,应该从深层存储器中读取数据。我正在为我的项目使用guicedi

有一个深层的存储已经编写和未来作为一个外部依赖。我正在努力用Guice实例化客户机

这是密码

作业模块

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);
}
公共接口读取器{
列表getData(字符串路径);
}
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);
  }
}
公共类DeepStoreReader实现读卡器{
私人最终DeepStoreClient DeepStoreClient;
DeepStoreReader(@Named(“config”)config){
this.deepStoreClient=新的deepStoreClient(配置);
}
@凌驾
公共列表getData(字符串路径){
返回此.deepStoreClient.getData(路径);
}
}
问题是我不想在
DeepStoreReader
构造函数中实例化
DeepStoreClient
,因为测试
DeepStoreReader
变得很困难,因为我无法模拟
DeepStoreClient

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

PS:我对DI和learning Guice不熟悉,您需要的是,例如:

Guice将负责为您实例化
DeepStoreClient

编辑:

如果
DeepStoreClient
本身具有依赖项,则还可以注释该构造函数:

@Inject
public DeepStoreClient(@Named("config") Config config) {
    // ... 8< ...
}
@Inject
公共DeepStoreClient(@Named(“config”)config){
// ... 8< ...
}

如果DeepStoreClient在构造函数中接受一个参数怎么办?让我编辑一下示例,但是,DeepStoreClient是一个外部发布的依赖项,不是Guice模块/实现。在这种情况下,您可能需要研究使用。但是,必须在模块中定义提供,并且DeepStoreClient非常特定于DeepStoreReader实现。这不是矛盾的关注点分离吗?如果这是一个关注点,您可以使用
DeepStoreReader
的私有子模块。政府将获得更多信息。
@Inject
public DeepStoreClient(@Named("config") Config config) {
    // ... 8< ...
}