我需要根据运行的环境获取my.application.dev,my.application.qa或my.application:
我的应用程序属性如下:
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.name}}设置为env **
Properties props = new Properties();
props.load(input);
String myValue = props.getProperty("application.property");
有什么主意吗?谢谢
答案 0 :(得分:2)
在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();
}