亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 237 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號