Here’s a quick look at how to read Play Framework application.conf configuration properties.
Update: This post applies to the Play Framework prior to version 2.5. As the comment below the article states, play.Play.application has been deprecated in Play 2.5. (I haven’t used Play in a little while, so I don’t know what the changes are in the new versions.)
First, given a Play application.conf file with these properties:
foo=42 bar.baz=10 stations = [ 99.5, 102.3, 104.3, 105.9 ] streams = [ { "name": "104.3", "file": "104_3.pls" }, { "name": "WGN", "file": "wgn.pls" } ]
You can read the foo
property (as a String
) from your Play code like this:
val foo = play.Play.application.configuration.getString("foo") println(s"foo = $foo (STARTUP)")
You can read the bar.baz
property like this:
val barBaz = play.Play.application.configuration.getString("bar.baz") println(s"barBaz = ${barBaz} (STARTUP)")
You access the stations
properties array like this:
import scala.collection.JavaConversions._ // the list of radio stations (99.5, 102.3, etc.) play.Play.application.configuration.getDoubleList("stations").foreach { station => println(s"STATION: $station") }
Note that getDoubleList
returns the stations as a List[Double]
.
Finally, you read the list of streams
like this:
import scala.collection.JavaConversions._ // the list of streams ("104.3" is "104_3.pls", etc.) play.Play.application.configuration.getConfigList("streams") foreach { stream => println(s" ${stream.getString("name")} is ${stream.getString("file")}") }
I did all of this in the Global
object in my application, but you can do the same thing from a Play Framework model, controller, or other code.
Note that there are other methods available besides getString
, getDoubleList
, and getConfigList
. See the API docs for other methods that are available.