gpt4 book ai didi

java - apache PropertiesConfiguration 无法解析占位符

转载 作者:行者123 更新时间:2023-12-02 01:08:20 24 4
gpt4 key购买 nike

假设我有以下两个配置文件:

文件1:

key1 = ${common.key1}
key2 = ${common.key2}

文件2:

common.key1 = value1
common.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 = value1
common.key2 = value2
key1 = ${common.key1}
key2 = ${common.key2}

为什么占位符没有解析?

最佳答案

参见this page在文档中,它说:

Below is some more information related to variable interpolation users should be aware of:

  • ...

  • Variable interpolation is done by all property access methods. One exception is the generic getProperty() method which returns the raw property value.

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

API docs of getProperty()也提到了这一点:

Gets a property from the configuration. ... On this level variable substitution is not yet performed.

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

关于java - apache PropertiesConfiguration 无法解析占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57711195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com