Play.current is deprecated in play 2.5

早过忘川 提交于 2019-11-30 16:57:39

问题


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
  }
}

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

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

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

Thanks


回答1:


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

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"))
  }

}

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



来源:https://stackoverflow.com/questions/36833420/play-current-is-deprecated-in-play-2-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!