Play 2 doesn't force you to use any particular method to manage your environments. But it provides you with powerful and flexible tools to implement it yourself according to the needs of your project.
For instance a common pattern is keeping common environment settings in one file and having environment-specific overrides in other files. In order to do that you will need a custom Global object (you can put it right into the ./app/Global.scala
). The following code is valid as of Play 2.1.1 (Scala 2.10):
import java.io.File
import play.api._
import com.typesafe.config.ConfigFactory
object Global extends GlobalSettings {
override def onLoadConfig(config: Configuration, path: File, classloader: ClassLoader, mode: Mode.Mode): Configuration = {
val modeSpecificConfig = config ++ Configuration(ConfigFactory.load(s"application.${mode.toString.toLowerCase}.conf"))
super.onLoadConfig(modeSpecificConfig, path, classloader, mode)
}
}
Now you can put application.dev.conf
, application.test.conf
, and application.prod.conf
into your ./conf
with environment specific overrides (while keeping common settings in application.conf
).
In this sample we're relying on the Play's own mode
which usually makes sense, but you can be as granular as you want and use environment variables or whatever you feel like.
See also: Typesafe Config
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…