播放2.5中不推荐使用play.current [英] Play.current is deprecated in play 2.5

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

问题描述

我目前正在通过以下方式使用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

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

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