我目前正在以下列方式使用 Play.current。
import play.api.{Logger, Play}
object ApplicationConfig {
val app = Play.current
def getConfInt(key: String): Option[Int] = {
val result = app.configuration.getInt(key)
result
}
}
自从迁移到 2.5 以来,我有一个警告说它已被弃用
"This is a static reference to application, use DI instead"
但是,该文档并没有确切说明我应该如何使用 DI。
谢谢
最佳答案
根据您的用例,您现在应该使用 Environment
, ApplicationLifecycle
和 Configuration
而不是 Application
在您的情况下,您实际上对配置感兴趣,因此在 Play 2.5.x 中执行此操作的方法如下:
class HomeController @Inject() (configuration: play.api.Configuration) extends Controller {
def config = Action {
Ok(configuration.underlying.getInt("some.config.key"))
}
}
我提供的示例适用于 Controller ,但您也可以在应用程序的其他地方使用这种方法。我只是不喜欢
ApplicationConfig
您提供的对象 - 在迁移到 Play 2.5.x 时考虑重构它 - DI 现在是要走的路
关于playframework - Play.current 在 play 2.5 中被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36833420/