Apache commons-configuration setDelimiterParsingDisable不生效的处理

 2023-02-25    332  

Apache commons-configuration setDelimiterParsingDisable不生效的处理

项目中有用到commons-configuration,版本1.9。

配置初始化大概这样:

Apache commons-configuration setDelimiterParsingDisable不生效的处理

CombinedConfiguration config = new CombinedConfiguration();
PropertiesConfiguration propConfig = new PropertiesConfiguration("filename...");
config.append(propConfig);
....
String value = config.getString("test.key");

在属性配置文件(×.properties)中,如下设置:

test.key=value1,value2

经调用发现value的值为”value1″,通过查看源码发现,在PropertiesConfiguration中有个delimiterParsingDisable属性,它来判禁是否用自动解析List的标志,默认是解析的。当getString的时候,会取list的第一个元素。所以可以解释了为什么只获取到了value1。知道为什么了,解决办法就有了,只要将delimiterParsingDisable这个属性设置为true就不会出现这种问题了。于是有了下面的代码:

CombinedConfiguration config = new CombinedConfiguration();
PropertiesConfiguration propConfig = new PropertiesConfiguration("filename...");
propConfig.setDelimiterParsingDisable(true);
config.append(propConfig);

再次测试,发现依然不起作用,于是debug代码,发现当调用这一步的时候new PropertiesConfiguration("filename...");内部已经完成了加载,也就是说,再设置这个值是没有意义的。于是有了最终的解决办法,就是在加载数据之前,将delimiterParsingDisable这个属性设置为true。最终代码如下:

private static class PropertiesConfigurationWrapper extends PropertiesConfiguration {
    public PropertiesConfigurationWrapper(String fileName) throws ConfigurationException {
        super();
        super.setDelimiterParsingDisabled(true);	// 设置不解析list
        super.setFileName(fileName);
        super.load(); // 此处为加载数据的地方

    }
}
以上所述是小编给大家介绍的Apache commons-configuration setDelimiterParsingDisable不生效的处理,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/34471.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。