在 play 2.4 中,覆盖 ApplicationLoader 中的 builder 方法或在 Abstract 模块中实现 EagerBinding 替换了现有的 play 2.3 GlobalSettings onStart。
但是在 play 2.3 onStart 方法中,您的应用程序已经启动并加载了所有插件/依赖项。你能在play 2.4 中做同样的事情吗,即在应用程序启动后运行一段代码。
在我的情况下,Slick 要求应用程序在访问数据库之前已经启动。
谢谢
在 play 2.4 中,覆盖 ApplicationLoader 中的 builder 方法或在 Abstract 模块中实现 EagerBinding 替换了现有的 play 2.3 GlobalSettings onStart。
但是在 play 2.3 onStart 方法中,您的应用程序已经启动并加载了所有插件/依赖项。你能在play 2.4 中做同样的事情吗,即在应用程序启动后运行一段代码。
在我的情况下,Slick 要求应用程序在访问数据库之前已经启动。
谢谢
众所周知,当您在 a 中急切地绑定一个类时,Module
它会尝试尽快初始化它的实例。在 play framework 2.4 中,这是您在应用程序启动之前获取运行代码的方式。
但遵循 DI 的常见预期规则:如果在要运行的类的构造函数中添加作为参数(也称为“依赖项”),app: Application
那么它将在应用程序启动后执行;像这样:
import play.api.Application
import javax.inject.Inject
class MyInitCodeClass @Inject() (val app: Application) {
//YOUR CODE HERE
}
这是合乎逻辑的,因为任何值得一提的 DI 框架都会计算出他可以按什么顺序注入哪些类。
然后在你的模块中添加通常的绑定(这里是 playframework 风格而不是 Guice):
bind[MyInitCodeClass] toSelf eagerly()
希望这有效。Play.current
停止使用并仅使用 Play 2.4 的新 DI 系统注入应用程序也很有用。
我想感谢 @easel 在playframework gitter room上帮助我解决这个问题。
Roy, didn't quite get your problem.
Do you have an issue with using an EagerBinding as you mention?
You can still use GlobalSettings onStart, beforeStart etc if you wish, its just discouraged because of the wish to move away from Global state.