Understanding the game 2.4. Dependency Injection

I have problems to understand how to apply the new Injection Dependency function in Play 2.4. I am familiar with Guice and skipped the Play Documentation explanation of how and when the actual binding occurs. I read the white paper [1] and tried to use the latest Play Mailer [2] as an example. The Mailer replay example uses an arbitrary class and annotates the MailerClient property with @inject . When I try to use an object of this class, the property is null , at least when debugging. So where and when do I need to do the actual injection? I have the same problem for @singleton annotation. It just explains how to comment on it, but not how to get the object. Should I use Guice directly or is it integrated somehow?

[1] https://www.playframework.com/documentation/2.4.x/JavaDependencyInjection

[2] https://github.com/playframework/play-mailer

+6
source share
1 answer

I believe the binding is through the MailerModule added to play. modules.enabled . MailerModule provides Guice bindings for MailerClient.

 play { modules { enabled += "play.api.libs.mailer.MailerModule" } 

For Guice to inject MailerClient into your object, it must be created through Guice. For example, if you want to use @Inject MailerClient in the controller or the service entered into the controller, your controller must be entered through Guice. The recommended approach for this in Play 2.4 adds the following to your build.sbt:

 routesGenerator := InjectedRoutesGenerator 
+1
source

All Articles