我需要根据运行环境获取 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/