【问题标题】:Play.current is deprecated in play 2.5Play.current 在 play 2.5 中已弃用
【发布时间】:2016-08-18 10:07:30
【问题描述】:

我目前正在通过以下方式使用 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 后,我收到一条警告说它已被

弃用

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

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

谢谢

【问题讨论】:

    标签: playframework playframework-2.5


    【解决方案1】:

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

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

    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 现在是要走的路

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    相关资源
    最近更新 更多