2
votes

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.

2
Probably there is an injection framework being used. which one?Ivan Stanislavciuc
Yes, guice, I updated the question thanks for your feedback ;)Axel Borja

2 Answers

3
votes

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.

0
votes

Having @Provides and @Singleton on the provideDummyService method is enough. Guice will find methods annotated with @Provides and do the wiring for you. It does this using Java Reflection. There is no need to add anything in the configure() method.

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