how to extend a list in spring config

Viewed 27142

I have defined a with some "common" values. How can I extend the common list by additional values to various new beans?

<util:list id="myCommonList" list-class="java.util.LinkedList">
 <bean .../>
 <bean .../>
 <bean .../>
 <bean .../>
</util:list>


<bean id="extension" parent="myCommonList">
  <bean ref="additionalValue"/>
</bean>

Will this overwrite the list or extend it?

6 Answers

To append list2's elements to list1 use the following spring config.

<util:list id="list1">
    <value>1</value>
<util:list>

<util:list id="list2">
    <value>3</value>
    <value>5</value>
<util:list>

<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="list1" />
    <property name="targetMethod" value="addAll" />
    <property name="arguments" ref="list2" />
</bean>
Related