开发者

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

开发者 https://www.devze.com 2023-02-10 16:00 出处:网络
I\'ve configuredin Spring listA (see below). It would开发者_开发知识库 be nice to have another one that contains all values from listA and expand it.

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

精彩评论

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