You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Guice中@ImplementedBy与模块顺序引发异常问题咨询

Guice模块绑定顺序引发异常的问题分析

代码定义

@ImplementedBy(MyClass.class)
public interface MyInterface {}

public class MyClass implements MyInterface {
  @Inject
  MyClass(MySecondClass mySecondClass) {}
}

public class MySecondClass {}

问题现象

按以下顺序创建Guice注入器时,会抛出异常:A just-in-time binding to MySecondClass was already configured on a parent injector.

Guice.createInjector(
    binder -> binder.bind(MyInterface.class),
    binder -> binder.bind(MySecondClass.class)
);

但交换两个模块的绑定顺序后,程序可以正常运行,无异常抛出:

Guice.createInjector(
    binder -> binder.bind(MySecondClass.class),
    binder -> binder.bind(MyInterface.class)
);

结论

这确实是Guice的已知BUG,并非使用方式错误。异常信息描述不合理(当前场景不存在父注入器),且绑定顺序本不应该影响注入器的创建结果。

补充说明

该BUG已存在至少十年,不仅createInjector方法传入的模块顺序会引发问题,单个模块的configure方法内部的绑定顺序也会导致同样的异常。

内容的提问来源于stack exchange,提问作者cpp beginner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 18:35:31