Apache commons-configuration setDelimiterParsingDisable不生效的处理

Apache commons-configuration setDelimiterParsingDisable不生效的处理

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

配置初始化大概这样:

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(); // 此处为加载数据的地方

    }
}

转载于:https://www.cnblogs.com/liushijie/p/5689639.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值