Search code examples
javascaladependency-injectionplayframeworkguice

how guice just in time binding works in play framework?


How do I inject the dependencies I have created in Play application. I also do not understand looking at the examples how they are injected without defining any binding.

https://github.com/manuelbernhardt/reactive-web-applications/blob/master/CH04/app/controllers/Quiz.scala

Can anyone explain with reference to this example. How is object of VocabularyService injected?


Solution

  • This question is more about dependency injection than it is about the Play Framework.

    The reason that you don't have to define an explicit binding for the VocabularyService is that Guice can find it for you:

    When a dependency is requested but not found it attempts to create a just-in-time binding.

    from: Guice Bindings

    On the documentation of just-in-time bindings you can see that the option for creating a just-in-time binding are Eligible Constructors, @ImplementedBy and @ProvidedBy.

    In our case, the class VocabularyService has a default non-private, no-arguments constructor.