Play.current 在 play 2.5 中被弃用 [英] Play.current is deprecated in play 2.5

查看:20
本文介绍了Play.current 在 play 2.5 中被弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过以下方式使用 Play.current.

I am currently using Play.current in the following way.

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 以来,我有一个警告说它已被

Since migrating to 2.5, I have a warning saying that it is deprecated with

这是对应用程序的静态引用,请改用 DI"

"This is a static reference to application, use DI instead"

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

However, the doc doesn't exactly say how am I supposed to use DI instead.

谢谢

推荐答案

根据您的用例,您现在应该使用 EnvironmentApplicationLifecycleConfiguration 而不是 Application

Depending on your use case you should now use Environment, ApplicationLifecycle and Configuration instead of Application

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

In your case you are actually interested in the configuration so the way to do this in Play 2.5.x would be like this:

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

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

}

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

The example I provided is for a controller but you can use this approach also on other places in your application. I just didn't like the ApplicationConfig object you provided - consider refactoring it when migrating to Play 2.5.x - DI is now the way to go

这篇关于Play.current 在 play 2.5 中被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆