Guice Names Bindings with Play Framework

Home / General / Guice Names Bindings with Play Framework

Play framework is an event driven framework by Lightbend and is a great choice for scalable application design.

Play uses Google Guice as it’s Dependency Injection framework, which is flagship and one of the most used libraries by google for Java eco system.

Guice is Annotation driven and fits nicely with new module architecture of play. For your own application you can do advance dependency bindings in for your project.

While Guice and Play combination is great, it is still somewhat tricky to get the few unique cases of DI right, we will check one such case here.

A sample looks like this,


But consider the use-case where depending upon some runtime attribute you wish to load a different implementation of an interface not by @Inject annotation but programmatically. In Guice you can do so as listed below:


But in play Key class and Guice injector are not directly available and are wrapped in BiindingKey and PLay’s own injector, so you can still can same result by using these classes like,


This will enable injecting different implementation dynamically into you code with Guice and Play.

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *