java - 将 jar 文件中的属性文件外部化
问题描述
我有一个带有 application.properties 文件的 jar,如下所示-
BASE_ENVIRONMENT = http://localhost
这是我读取属性文件的实用程序类
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream in = loader.getResourceAsStream(fileName + ".properties");
属性文件在<default package>
这个 jar 在我的 Web 应用程序中用作依赖 jar,该应用程序部署在tomcat server
.
但我需要更改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。
推荐阅读
- css - svg:样式化路径
- java - FragmentManager 在没有容器 ID 的情况下开始事务
- python-3.x - 可以从 Amazon SP-API 获取美国和加拿大的库存数据,但不能从英国获取
- javascript - 来自多个文件的一个 Promise
- python - 第一次从 CPanel 使用 API 时出现 401 错误
- qt - QtQuickControls FileDialog 自动化
- java - 如何通过锁定/同步的对象循环链表?
- gcc - gcc 不尊重我的包含目录顺序
- algorithm - 您如何使用比较函数对列表进行排序,其中 a 和 b 的值是提前知道的?
- c# - 设置 DevExpress ComboBox 的 ListControl 的高度