播放2.5中不推荐使用Play.current

Sci*_*ion 8 playframework playframework-2.5

我目前正在以下列方式使用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
  }
}
Run Code Online (Sandbox Code Playgroud)

自从迁移到2.5后,我有一个警告说它已被弃用

"这是对应用程序的静态引用,而是使用DI"

但是,文档并没有说明我应该如何使用DI代替.

谢谢

Ant*_*ton 6

根据您的使用情况,您现在应该使用Environment,ApplicationLifecycleConfiguration不是Application

在您的情况下,您实际上对配置感兴趣,因此在Play 2.5.x中执行此操作的方式如下:

class HomeController @Inject() (configuration: play.api.Configuration) extends Controller {

  def config = Action {
    Ok(configuration.underlying.getInt("some.config.key"))
  }

}
Run Code Online (Sandbox Code Playgroud)

我提供的示例是用于控制器,但您也可以在应用程序的其他位置使用此方法.我只是不喜欢ApplicationConfig你提供的对象 - 考虑在迁移到Play 2.5.x时重构它 - DI现在是要走的路