Java 在Guice中将多个接口自动绑定到一个impl

Java 在Guice中将多个接口自动绑定到一个impl,java,guice,Java,Guice,我有一个如下所示的设计,一个接口扩展多个父接口,一个接口的实现 在我的客户机类中,我只希望依赖一个或多个父接口,而不是ZooKeeperClient。我觉得这是一个更好的设计,因为它减少了客户机类依赖项的表面积,并且使在测试中模拟事物变得更容易 e、 g 但是,为了实现这一点,我需要手动将绑定从每个接口添加到实现类: bind(ServiceCreator.class).to(ZooKeeperClientImpl.class) bind(ServiceDeleter.class).to(Zo

我有一个如下所示的设计,一个接口扩展多个父接口,一个接口的实现

在我的客户机类中,我只希望依赖一个或多个父接口,而不是
ZooKeeperClient
。我觉得这是一个更好的设计,因为它减少了客户机类依赖项的表面积,并且使在测试中模拟事物变得更容易

e、 g

但是,为了实现这一点,我需要手动将绑定从每个接口添加到实现类:

bind(ServiceCreator.class).to(ZooKeeperClientImpl.class)
bind(ServiceDeleter.class).to(ZooKeeperClientImpl.class)
bind(ServiceUpdater.class).to(ZooKeeperClientImpl.class)
// ...
bind(ZooKeeperClient.class).to(ZooKeeperClientImpl.class)
我有没有办法避免这种重复,并告诉Guice立即绑定整个层次结构?类似于

bind(ZooKeeperClient.class/* and its parents*/).to(ZooKeeperClient.class)

如果没有,我的设计有什么问题吗?我在做一些非Guicy的事情吗?

在Guice中没有这种方法,您可以使用类似于迭代所有接口并绑定它们的实用程序。

在中,您可以对实现类型进行操作

autobind(ZooKeeperClientImpl.class).toConstructor();
这将把类绑定到它的所有接口和超类(除了
对象
)。这些绑定比显式绑定弱-因此将
ZooKeeperClientImpl
超级类型之一绑定到其他类型

bind(ServiceUpdater.class).to(AnotherImplementation.class);
将控制自动绑定,这样您就不会因为不明确的绑定而发生冲突


Silk与Guice非常相似,所以如果您不需要编写太多Guice代码,那么就可以轻松快速地进行更改

谢谢你的确认和链接。以前从没听说过丝绸。我将在下一个项目中尝试一下。谢谢
bind(ServiceUpdater.class).to(AnotherImplementation.class);