使用 Apache Commons Configuration 进行变量插值,即 ${variable},在属性文件中包含值列表 [英] Using Apache Commons Configuration to do variable interpolation, i.e. ${variable}, with a list of values in a properties file

查看:60
本文介绍了使用 Apache Commons Configuration 进行变量插值,即 ${variable},在属性文件中包含值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache Commons Configuration 来读取属性文件,并且我完全能够进行变量插值,并且还可以将多值属性作为列表进行检索.但是,我无法正确加载具有多个值的属性,其中一个值是另一个多值属性的引用(变量插值).

I am using Apache Commons Configuration to read a properties file, and I am perfectly able to do variable interpolation, and also to retrieve multi-valued properties as a list. However, I haven't been able to correctly load a property which has several values, where one of them is a reference (variable interpolation) to another multi-valued property.

这是我的属性文件的示例(我也尝试过使用逗号分隔语法):

Here's an example of my properties file (I have also tried using comma-separated syntax):

doc.mime=application/msword
doc.mime=application/vnd.openxmlformats-officedocument.wordprocessingml.document
doc.mime=${office.mime}

office.mime=application/x-tika-msoffice
office.mime=application/x-tika-ooxml

以及我如何从中阅读:

Configuration config = new PropertiesConfiguration("myFile");
final String[] mimesArray = config.getStringArray("doc.mime");
for(String mime : mimesArray) System.out.println(mime);
final List<Object> mimesList = config.getList("doc.mime");
System.out.println(mimesList);

这是我使用任一方法(getStringArraygetList)获得的内容:

This is the content I get with either method (getStringArray and getList):

[application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/x-tika-msoffice]

这和我预想的不一样:doc.mimeoffice.mime

This is different from what I expected: the complete contents of both doc.mime and office.mime

有谁知道是否可以在我的另一个列表中插入整个值列表?如果是这样,它是如何完成的?

Does anyone know if it is possible to interpolate the whole list of values in my other list? And if so, how is it done?

推荐答案

Commons Configuration 的作用

如您所见:在插入多值属性时,Commons Configuration 只会解析该属性的第一个值.请参阅 AbstractConfiguration#resolveContainerStore() 第 1177 行.

As you found out: When interpolating a multi-valued property Commons Configuration will only resolve the first value of that property. See the code at AbstractConfiguration#resolveContainerStore() line 1177.

我发现了一些相关问题:

I found some related issues:

CONFIGURATION-28:有人想要(并得到)完全相反的你想要什么:只有多值属性中的第一个值.

CONFIGURATION-28: Someone wants (and gets) the exact opposite of what you want: Only the first value in a multi-valued property.

CONFIGURATION-55:更多关于多值属性插值的讨论:

CONFIGURATION-55: More discussion about interpolation of multi-valued properties:

这个问题可能没有正确的解决方案,因为预期结果在很大程度上取决于具体用例

there is probably no correct solution to this problem because the expected results strongly depend on a concrete use case

解决方法:在代码中合并两个列表

绝对比自定义插值容易:

List<Object> mimesList = config.getList("doc.mime");
List<Object> officeList = config.getList("office.mime");
mimesList.addAll(officeList);
System.out.println(mimesList);

在 Commons Configuration 项目中提出这个问题

改变整个变量插值系统可能很困难.但他们至少可以澄清文档.

Changing the whole variable interpolation system is probably difficult. But they could at least clarify the documentation.

这篇关于使用 Apache Commons Configuration 进行变量插值,即 ${variable},在属性文件中包含值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆