crockpotveggies crockpotveggies - 1 month ago 10
Scala Question

Access Play! 2.0 configuration variables in application.conf?

Previously in Play! v1 it was really easy to define configuration variables in

application.conf
and then access them like so:

play.configuration("db.driver")


However now I can't find anything in the documentation for similar uses in v2 or a proper alternative. What's the method for doing so? Thanks!

Answer

As of Play 2.5, play.api.Play.current is deprecated. You should use dependency injection to inject the Environment or Configuration and use that to read the configuration value:

class HomeController @Inject() (configuration: play.api.Configuration) extends Controller {
  def config = Action {
    Ok(configuration.underlying.getString("db.driver"))
  }
}

Checkout the Play documentation for more detailed discussion.

Comments