如何在play 2.0的多个环境中管理application.conf? [英] How to manage application.conf in several environments with play 2.0?

查看:111
本文介绍了如何在play 2.0的多个环境中管理application.conf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Play 1.2中,我可以为配置项添加框架ID或应用程序模式的前缀,如下所示:

With Play 1.2, I can prefix the configuration keys with the framework ID or application mode as follows:

# Production configuration
%prod.http.port=80
%prod.application.log=INFO
%prod.application.mode=prod

但是它似乎不适用于2.0.

But it doesn't seem to work with 2.0.

有什么办法可以使它工作?

Is there any way to make it work?

推荐答案

播放2不会强制您使用任何特定方法来管理您的环境.但是它为您提供了强大而灵活的工具,可以根据您的项目需求自行实施.

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.

例如,一种常见的模式是在一个文件中保留通用的环境设置,而在其他文件中保留特定于环境的替代.为此,您需要一个自定义全局对象(可以将其放置进入./app/Global.scala).以下代码自Play 2.1.1(Scala 2.10)起有效:

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

现在,您可以将application.dev.confapplication.test.confapplication.prod.conf放入具有特定于环境的替代项的./conf中(同时将常见设置保留在application.conf中).

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).

在此示例中,我们依赖于Play自己的mode,通常这是有道理的,但是您可以根据需要进行任意设置,并使用环境变量或任何您喜欢的方式.

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.

另请参见:类型安全配置

这篇关于如何在play 2.0的多个环境中管理application.conf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆