为了账号安全,请及时绑定邮箱和手机立即绑定

apache PropertiesConfiguration 无法解析占位符

apache PropertiesConfiguration 无法解析占位符

RISEBY 2023-06-28 15:56:30
假设我有以下两个配置文件:文件一:key1 = ${common.key1}key2 = ${common.key2}文件2:common.key1 = value1common.key2 = value2我有以下代码:import org.apache.commons.configuration.PropertiesConfiguration;...PropertiesConfiguration newConfig = new PropertiesConfiguration();File configFile1 = new File("...paht to file 1");File configFile2 = new File("...path to file 2");newConfig.setDelimiterParsingDisabled(true);newConfig.load(configFile2);newConfig.load(configFile1);Iterator<String> props = newConfig.getKeys();while (props.hasNext()) {    String propName = props.next();    String propValue = newConfig.getProperty(propName).toString();    System.out.println(propName + " = " + propValue);}我有以下输出:common.key1 = value1common.key2 = value2key1 = ${common.key1}key2 = ${common.key2}为什么占位符没有解析?
查看完整描述

2 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

以下是用户应注意的与变量插值相关的更多信息:

  • ...

  • 变量插值由所有属性访问方法完成。一个例外是返回原始属性值的通用方法。getProperty()

这正是您在代码中使用的。

API 文档getProperty()也提到了这一点:

从配置中获取属性。...在此级别上尚未执行变量替换。

使用其他可用的方法来PropertiesConfiguration获取实际的插值。例如,调用getProperties()将其PropertiesConfiguration转换为java.util.Properties对象并对其进行迭代。


查看完整回答
反对 回复 2023-06-28
?
12345678_0001

TA贡献1802条经验 获得超5个赞

也可以通过占位符替换以通用方式使用它,如下所示:

config.get(Object.class, propName);

getProperty与方法不同,get带参数的方法Object.class将返回原始类的值,并插入变量。


查看完整回答
反对 回复 2023-06-28
  • 2 回答
  • 0 关注
  • 127 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信