Access Play! 2.0 configuration variables in application.conf?

Solution 1:

The Play 2.0 Scala equivalent to this would be:

Play.current.configuration.getString("db.driver")

You will also need import play.api.Play

The full docs for this are here.

Solution 2:

Applies to Play 2.0 - In Java Controller you can use following:

String optionValue = Play.application().configuration().getString("db.driver");

To get variable in view use this:

@play.Play.application().configuration().getString("db.driver")

Solution 3:

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.

Solution 4:

In Play 2.3.2 for Java you can use the com.typesafe.config.ConfigFactory option:

Config conf = ConfigFactory.load();
String myFooBarConfiguration = conf.getString("foo.bar");

Fast moving API!