We have a new Play 2.0 project and we are planning to introduce DI as we add some complex 3rd party integration code.
There is a Guice plugin for Play 2.0 but it looks like it will be obsolete at 2.1 and I have a hunch that 2.1 is not that far anymore.
https://github.com/typesafehub/play-plugins/tree/master/guice
Is Guice a safe bet for Play 2.0/2.1 or should we consider other options?
I'd go with the 2.1 version, and the new controller instanciation from the Global object.
Here is a Guice example from the doc:
import play.GlobalSettings;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class Global extends GlobalSettings {
private static final Injector INJECTOR = createInjector();
@Override
public <A> A getControllerInstance(Class<A> controllerClass) throws Exception {
return INJECTOR.getInstance(controllerClass);
}
private static Injector createInjector() {
return Guice.createInjector();
}
}
You have to declare a special route for these controllers, with the special @
:
GET /myUrl @controllers.MyController.myMethod()
And you can also take a look at this demo using Spring: https://github.com/guillaumebort/play20-spring-demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With