我想根据运行环境将我的 Play 应用程序指向特定的应用程序配置文件。共有三个,它们对应于标准 Play 状态:
- 应用程序.dev.conf
- 应用程序.test.conf
- application.prod.conf
一位同事分享了执行此操作的方法,该方法需要设置操作系统环境变量。
我想消除设置操作系统变量的需要。 我的偏好是使用 Play 在启动时使用的任何内容来了解它所处的模式。
例如,如果您从命令行执行play run
,部分输出为"[info] play - Application started (Dev)"
我想在我的 Global.java 中使用此信息,我在其中覆盖了 onLoadConfig,如下所示:
public Configuration onLoadConfig(Configuration baseConfiguration, File f, ClassLoader loader) {
String playEnv=<some static method to get mode>;
Config additionalConfig = ConfigFactory.parseFile(new File(f,"conf/application."+playEnv+".conf"));
Config baseConfig = baseConfiguration.getWrappedConfiguration().underlying();
return new Configuration(baseConfig.withFallback(additionalConfig));
}
我发现的所有内容都是如何在应用程序启动后执行此操作,即使用 isDev()、isTest()、isProd()。
当我在 Global.java 中覆盖 onLoadConfig 时,是否有提供模式的静态方法?
最佳答案
我认为play run
是dev,play start
是prod。
编辑:如果您想查看当前模式是什么,它是通过 play.api.Play.current 传入的:
Play.current.mode match {
case Play.Mode.Dev => "dev"
case Play.Mode.Prod => "prod"
}
关于java - 在使用 Java 启动应用程序之前检测 Play 2.2.x 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19572447/