【问题标题】:How can I map property file items to Map object in Spring Application Context xml?如何将属性文件项映射到 Spring Application Context xml 中的 Map 对象?
【发布时间】:2013-10-16 08:55:30
【问题描述】:

我有一个这样的属性文件

com.country.<COUNTRY>=<values comma seperated>

例如:

com.country.UK=100,200
com.country.US=10,20

现在将来可以添加许多其他国家/地区条目 我可以通过 propertyPlaceHolder 将这些值输入到 Spring 应用程序上下文 xml 中的 Map&lt;String,List&lt;Integer&gt;&gt; 吗?

【问题讨论】:

    标签: java spring dictionary properties


    【解决方案1】:

    你可以使用spring util: 不要忘记导入方案,使用 util:map 和 util:list 来构建你想要的任何结构。可以使用 ref-ids 作为值:

    <beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
            xsi:schemaLocation="http://www.springframework.org/schema/util 
            http://www.springframework.org/schema/util/spring-util-2.5.xsd">
    
    <util:map id="map1" map-class="java.util.HashMap">
        <entry key="smthgkey" value="smthvalue"/>
    </util:map>
    
    <util:list id="list1" list-class="java.util.ArrayList">
        <value>val1</value>
    </util:list>
    

    另一种方法是创建具有必要结构的bean,但我从未使用过这种方式。您可以在此处找到示例:How to define a List bean in Spring?

    【讨论】:

    • 但是如何映射我的动态属性名称和值?
    • 加载属性后,我知道重新加载它们的唯一方法是 refresh 上下文。也许最好创建自定义方法来直接从文件(或数据库)重新加载您的属性,而不需要冗余的上下文操作。
    猜你喜欢
    • 2014-09-06
    • 2023-04-02
    • 2013-01-16
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多