title: How to extend already defined lists and maps in Spring application context?
body: <p>Imagine a staged application context with different phases. We start with an early phase to define the necessary infrastructure. The xml application contexts are loaded sequentially. </p>
  
  <p>The reason to split up these files is an extension/plugin mechanism.</p>
  
  <p><strong>Stage 01-default-configuration.xml</strong></p>
  
  <p>We prepare and declare the map with id <code>exampleMapping</code> to enhance them later with data.</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:util="http://www.springframework.org/schema/util"
         xsi:schemaLocation="[...]"&gt;
  
    &lt;util:map id="exampleMapping" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Stage 02-custom-configuration.xml</strong> <em>(optional)</em></p>
  
  <p>We configure the <code>exampleMapping</code> and add an entry.</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:util="http://www.springframework.org/schema/util"
         xsi:schemaLocation="[...]"&gt;
  
    &lt;util:map id="exampleMapping"&gt;
      &lt;entry key="theKey" value="theValue" /&gt;
    &lt;/util:map&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Stage 03-make-use-of-configuration.xml</strong> <em>(mandatory)</em></p>
  
  <p>Uses the defined map <code>exampleMapping</code>, whether it's configured customly or it's still the empty declared map.</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="[...]"&gt;
  
      &lt;bean id="exampleService" class="com.stackoverflow.example.ExampleService"&gt;
         &lt;property name="mapping" ref="exampleMapping" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>The problem here is, that it's not possible to add entries to the <code>exampleMapping</code> map after the first stage. Spring throws an exception that the map with id <code>exampleMapping</code> already exists. If we leave out the first stage the map is undeclared and the third stage can't resolve <code>exampleMapping</code> which also produces an exception.</p>
  
  <p>How can I solve this issue? I read <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-collection-elements-merging" rel="noreferrer">Collection merging</a> (spring docs) but this didn't helped. Is it possible to add values later to maps/lists before using them?</p>
  
  <p>Thank you!</p>