[英]Set application.properties at runtime
I need to get my.application.dev, my.application.qa or my.application depending on the environment where is running: 我需要根据运行的环境获取my.application.dev,my.application.qa或my.application:
I have my application.properties like this: 我有这样的application.properties:
application.prod=my.application
application.qa=my.application.qa
application.dev=my.application.dev
application.property=${application.${env.name}}
And this is my code: 这是我的代码:
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");
** What could I do here is to set ${env.name}} with env** **我在这里可以做的是用env设置$ {env.name}}
Properties props = new Properties();
props.load(input);
String myValue = props.getProperty("application.property");
Any idea? 任何想法? Thanks 谢谢
In application.properties
just define: 在application.properties
只需定义:
application.prod=my.application
application.qa=my.application.qa
application.dev=my.application.dev
Then read the needed property: 然后阅读所需的属性:
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.