【问题标题】:Spring: DRY with <list/>. Copy all values from listA to listB春天:用 <list/> 干燥。将所有值从列表复制到列表
【发布时间】:2011-06-27 02:18:16
【问题描述】:

我已经在 Spring listA 中进行了配置(见下文)。如果有另一个包含 listA 中的所有值并展开它,那就太好了。

<bean id="listA" class="java.util.ArrayList">
    <constructor-arg>
        <list>
            <value>a</value>
            <value>b</value>
            <value>...</value>
            <value>z</value>
        </list>
    </constructor-arg>
</bean>

如何在Spring中重写这样的Java代码?

    List listB = new ArrayList(listA);
    listB.add("A");
    ...
    listB.add("Z");

【问题讨论】:

标签: java spring ioc-container dry


【解决方案1】:

你可以使用collection merging:

<bean id="listA" class="java.util.ArrayList">
    <constructor-arg index="0">
        <list>
            <value>a</value>
            <value>b</value>
            <value>...</value>
            <value>z</value>
        </list>
    </constructor-arg>
</bean>

<bean id="listB" parent="listA">
    <constructor-arg index="0">
        <list merge="true">
            <value>A</value>
            <value>...</value>
            <value>Z</value>
        </list>
    </constructor-arg>
</bean>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2016-01-29
    • 1970-01-01
    • 2015-06-07
    相关资源
    最近更新 更多