假设我有以下两个配置文件:
档案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}为什么占位符未被解析?
发布于 2019-08-29 14:27:34
请参阅文档中的此页,其中说:
下面是一些与变量插值用户应该知道的更多信息:
getProperty() 方法,它返回原始属性值.。这正是您在代码中使用的内容。
getProperty()也提到了这一点:
获取配置中的属性。.尚未在此级别上执行变量替换。
使用PropertiesConfiguration中可用的其他方法来获得实际的内插值。例如,调用PropertiesConfiguration上的PropertiesConfiguration将其转换为java.util.Properties对象并进行迭代。
https://stackoverflow.com/questions/57711195
复制相似问题