简体   繁体   English

在运行时设置application.properties

[英]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.

相关问题 无法使用application.properties设置值 - Failed to set value with application.properties 使用 application.properties 设置 jvm arguments - Using application.properties to set jvm arguments 寻找一种在应用程序运行时覆盖 application.properties 值的方法 - Looking for a way to override application.properties values during application runtime Spring缓存:在运行时通过application.properties启用/禁用缓存 - Spring Caching: Enable/Disable Cache via application.properties at runtime 如何在运行时编辑application.properties(供下次使用) - How to edit application.properties during runtime (for next time use) 如何在不属于jar的运行时中重新加载application.properties - How to reload application.properties in runtime which is not part of jar Spring如何在运行时从application.properties重新加载值 - Spring how to reload the values from application.properties at runtime 在运行Spring时观看application.properties的更改 - Watch application.properties changes at runtime Spring boot 使用 Spring 引导在运行时以编程方式更改 application.properties 文件 - Programatically changing the application.properties file at runtime with Spring Boot application.properties 中的注释 - Comments in application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM