gpt4 book ai didi

java - 在运行时设置 application.properties

转载 作者:行者123 更新时间:2023-11-30 01:46:06 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com