与 Guice 一起使用时,AbstractModule class 绑定的顺序是什么?

What is the order of AbstractModule class binding when used with Guice?

这个问题用一个例子可能更容易理解。

我正在使用 Guice 创建注入器:

  val injector = Guice.createInjector(new Module)

使用以下模块 class:

class Module extends AbstractModule {

  override def configure(): Unit = {
    val instance = aCallToGetAnInstance()
    bind(classOf[DummyClass]).toInstance(instance)
    bind(classOf[DummyClass2]).asEagerSingleton()
  }

  @Provides
  @Singleton
  def provideDummyService: DummyService = {
    DummyService.standard.build()
  }

}

这 3 个绑定 class 中的哪一个会先绑定?

如果以下调用之一注入另一个调用,则此问题似乎有意义 class。

感谢您的回答。

这就是注入框架为您所做的。

只要你的代码中没有任何循环guice就可以解决。

在启动时验证所有绑定(例如,如果您有循环,它会抱怨)。然而,实例化是在需要时(惰性) - 例外是 eager singleton.

如果我误解了你,请评论。

provideDummyService 方法上有 @Provides@Singleton 就足够了。 Guice 将找到用 @Provides 注释的方法并为您进行连接。它使用 Java 反射来做到这一点。 configure() 方法中无需添加任何内容。

https://github.com/google/guice/wiki/ProvidesMethods