java - 在使用 Java 启动应用程序之前检测 Play 2.2.x 模式

标签 java playframework playframework-2.2

我想根据运行环境将我的 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/

相关文章:

java - 如何在iText7中为单个页面设置页面标签?

java - 修剪或替换字符串中的所有逗号和空格

java - 使用 for 和 while 在求解可能的三角形总数时的区别

json - Playframework JSON 解析 - 空指针异常 - 当数组存在时

java - 在 postgresql 的 jpql 查询中参数化对象名称

java - 断言部分页面源代码不起作用

java.lang.OutOfMemoryError : unable to create new native thread in Java (Play Framework) 错误

java - 将 playframework 1.2.5 与现有数据库结合使用

java - Play Framework 2.2.2 - Java - 使用模拟对象测试 Controller

java - 在 Play Framework 中使用 JPA/Hibernate 创建表/查询