我有 jar 和 application.properties 文件如下-
BASE_ENVIRONMENT = http://localhost
这是我读取属性文件的实用程序类
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(fileName + ".properties");
属性文件在 <default package>
中
此 jar 在部署在 tomcat server
中的 Web 应用程序中用作依赖 jar .
但我需要更改 BASE_ENVIRONMENT
用于生产环境。
有没有办法可以外部化这个属性文件的值?
最佳答案
您可以添加一个系统参数作为配置文件:
// default to DEV
String profile = "dev"
if (System.getProperty("ENV") != null) {
profile = System.getProperty("ENV");
}
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(fileName + "_" + profile + ".properties");
然后你会开始你的应用程序
.... -DENV=prod
像 config_prod.properties 这样的文件会在类路径中找到,默认是 config_dev.properties。
关于java - 外部化 jar 文件中的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50858246/