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?
我用2.1版本,并从全局对象的新控制器instanciation去。
下面是从一个Guice的例子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();
}
}
你必须申报这些控制器一种特殊的路由,与特殊@
:
GET /myUrl @controllers.MyController.myMethod()
而且你还可以使用看看这个演示春季: https://github.com/guillaumebort/play20-spring-demo