繁体   English   中英

如何在属性文件中指定值,以便可以使用ResourceBundle#getStringArray检索它们?

[英]How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?

我试图使用ResourceBundle#getStringArray从属性文件中检索String[] 文档中对此方法的描述如下:

从此资源包或其父项之一获取给定键的字符串数组。

但是,我尝试将值作为多个单独的键/值对存储在属性文件中:

key=value1
key=value2
key=value3

并以逗号分隔的列表:

key=value1,value2,value3

但这些都不能使用ResourceBundle#getStringArray检索。

如何在属性文件中表示一组键/值对,以便可以使用ResourceBundle#getStringArray检索它们?

Properties对象可以保存Object ,而不仅仅是String 这往往被遗忘,因为它们绝大多数用于加载.properties文件,因此通常只包含String 该文档表明调用bundle.getStringArray(key)等同于调用(String[]) bundle.getObject(key) 这就是问题:值不是String[] ,它是一个String

我建议以逗号分隔格式存储它并在值上调用split()

您可以使用Commons Configuration ,它具有getListgetStringArray方法,允许您检索逗号分隔字符串列表。

嗯,看起来这是一个常见的问题,来自这里这里的线程。

看起来要么你不使用该方法并自己解析数组的值,要么自己编写自己的ResourceBundle实现并自行完成:(。也许有一个apache commons项目为此...

从JDK源代码来看,PropertyResourceBundle似乎不支持它。

例:

mail.ccEmailAddresses=he@anyserver.at, she@anotherserver.at

..

myBundle=PropertyResourceBundle.getBundle("mailTemplates/bundle-name", _locale);

..

public List<String> getCcEmailAddresses() 
{
    List<String> ccEmailAddresses=new ArrayList<String>();
    if(this.myBundle.containsKey("mail.ccEmailAddresses"))
    {
        ccEmailAddresses.addAll(Arrays.asList(this.template.getString("mail.ccEmailAddresses").split("\\s*(,|\\s)\\s*")));// 1)Zero or more whitespaces (\\s*) 2) comma, or whitespace (,|\\s) 3) Zero or more whitespaces (\\s*)
    }       
    return ccEmailAddresses;
}

我不相信从属性文件加载ResourceBundles可以实现这一点。 PropertyResourceBundle利用Properties类来加载属性文件。 Properties类将属性文件作为一组String-> String映射条目加载,并且不支持拉出String []值。

调用ResourceBundle.getStringArray只调用ResourceBundle.getObject,将结果转换为String []。 由于PropertyResourceBundle只是将其移交给从文件加载的Properties实例,因此您永远无法使用当前的库存PropertyResourceBundle。

只需使用spring - Spring .properties文件:将元素作为数组

相关代码:

base.module.elementToSearch=1,2,3,4,5,6

@Value("${base.module.elementToSearch}")
  private String[] elementToSearch;
key=value1;value2;value3

String[] toArray = rs.getString("key").split(";");
public String[] getPropertyStringArray(PropertyResourceBundle bundle, String keyPrefix) {
    String[] result;
    Enumeration<String> keys = bundle.getKeys();
    ArrayList<String> temp = new ArrayList<String>();

    for (Enumeration<String> e = keys; keys.hasMoreElements();) {
        String key = e.nextElement();
        if (key.startsWith(keyPrefix)) {
            temp.add(key);
        }
    }
    result = new String[temp.size()];

    for (int i = 0; i < temp.size(); i++) {
        result[i] = bundle.getString(temp.get(i));
    }

    return result;
}

我试过这个并找到了办法。 一种方法是定义ListresourceBundle的子类,然后定义String []类型的实例变量并将值赋给键..这里是代码

@Override
protected Object[][] getContents() {
    // TODO Auto-generated method stub

    String[] str1 = {"L1","L2"};

    return new Object[][]{

            {"name",str1},
            {"country","UK"}                
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM