开发者

Spring: DRY with <list/>. Copy all values from listA to listB

I've configured in Spring listA (see below). It would开发者_开发知识库 be nice to have another one that contains all values from listA and expand it.

<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>

How to rewrite in Spring such Java code?

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


You can use 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜