Search code examples
scalaguice

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


The question is probably more understandable with an example.

I am using Guice to create injector:

  val injector = Guice.createInjector(new Module)

with the following 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()
  }

}

Which of these 3 bound classes would be bound first?

This question seems to make sense if one of the following calls inject one of the other class.

Thanks for your answers.


Solution

  • That is what the Injection framework does for you.

    As long you don't have any cycle in your code guice can resolve it.

    At startup all the bindings are verified (it complains for example if you have cycles). The instantiation however is when needed (lazy) - the exception is eager singleton.

    Just comment if I misunderstood you.