播放2.0 / 2.1 for Java和依赖注入 [英] Play 2.0/2.1 for Java and dependency injection
问题描述
我们有一个新的Play 2.0项目,我们计划在添加一些复杂的第三方集成代码时引入DI。
We have a new Play 2.0 project and we are planning to introduce DI as we add some complex 3rd party integration code.
Play 2.0有一个Guice插件但看起来它在2.1时已经过时了,我预感2.1不再那么远了。
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
对于Play 2.0 / 2.1,Guice是安全的赌注,还是我们应该考虑其他选择?
Is Guice a safe bet for Play 2.0/2.1 or should we consider other options?
推荐答案
我请使用2.1版本,以及来自Global对象的新控制器实例。
I'd go with the 2.1 version, and the new controller instanciation from the Global object.
这是来自 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()
您还可以使用Spring查看此演示:< a href =https://github.com/guillaumebort/play20-spring-demo> https://github.com/guillaumebort/play20-spring-demo
And you can also take a look at this demo using Spring: https://github.com/guillaumebort/play20-spring-demo
这篇关于播放2.0 / 2.1 for Java和依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!