java - 在运行时设置 application.properties

标签 java application.properties

我需要根据运行环境获取 my.application.dev、my.application.qa 或 my.application:

我的 application.properties 像这样:

application.prod=my.application
application.qa=my.application.qa
application.dev=my.application.dev
application.property=${application.${env.name}}

这是我的代码:

InputStream input = this.getClass().getClassLoader().getResourceAsStream("application.properties");
// Let's say that I have a environment var to determinate if it's running in dev, qa or prod
// At this point, I do something like this String env = System.getenv("envName");

** 我在这里能做的就是用 env** 设置 ${env.name}}

Properties props = new Properties();
props.load(input);
String myValue = props.getProperty("application.property");

有什么想法吗?谢谢

最佳答案

application.properties中只需定义:

application.prod=my.application
application.qa=my.application.qa
application.dev=my.application.dev

然后读取所需的属性:

String env = System.getProperty("envName");
try (InputStream input = getClass().getClassLoader().getResourceAsStream("application.properties")) {
  Properties props = new Properties();
  props.load(input);
  String value = props.getProperty("application." + env);
  System.out.println(value);
} catch (IOException ex) {
  ex.printStackTrace();
}

关于java - 在运行时设置 application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57930176/

相关文章:

java - 远程运行 MapReduce

java - 如何从Spring Controller 中的webapp文件夹访问静态文件?

java - JUL 适配器不适用于 Jersey

spring-boot - Spring Boot management.trace.include配置实例

git - 从配置服务器获取application.properties时出现Redis连接失败错误

java - Spring boot项目如何保存DB用户名和密码

java - Android:Log Verbose 在 2.2 上针对新字符串 "byte array"初始化抛出错误

java - 确定 hashmap 内部数组列表的大小

kubernetes - Quarkus Kubernetes 运算符 : how to set environment variables in application container (not operator container)?

java - Quarkus 如何在 application.properties 中设置环境变量