<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=matthewadams" rel="nofollow">Matthew T. Adams</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8835?redirect=false" rel="nofollow">SPR-8835</a></strong> and commented</p>
<p dir="auto">I couldn't find a factory bean that combines maps, and the feature of merging collections among parent &amp; child beans (<a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-collection-elements" rel="nofollow">http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-collection-elements</a>) didn't suit my needs, so I created MapCombiningFactoryBean.  Please add to Spring core if you see fit.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.0.9, 2.5.6, 3.0.6, 3.1 RC1</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/19107/MapCombiningFactoryBean.java" rel="nofollow">MapCombiningFactoryBean.java</a> (<em>1.45 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398100553" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/11189" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/11189/hovercard" href="https://github.com/spring-projects/spring-framework/issues/11189">#11189</a> Introduce 'merge' attribute for util namespace collection elements (<em><strong>"duplicates"</strong></em>)</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=sbrannen" rel="nofollow">Sam Brannen</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-6523?redirect=false" rel="nofollow">SPR-6523</a></strong> and commented</p>
<p dir="auto">Section <em>C.2.2.4 util:list/</em> of the reference manual states:</p>
<blockquote>
<p dir="auto">Finally, you can also control the merging behavior using the 'merge' attribute of the util:list/ element; collection merging is described in more detail in the section called "Collection merging".</p>
</blockquote>
<p dir="auto">This is, however, not true. In contrast to the standard support for collections (i.e., array, list, set, map) in the <em>beans</em> namespace, the <em>util</em> namespace does not support the merge attribute for collections.</p>
<p dir="auto">Is this an oversight in the documentation or missing functionality in the util namespace support?</p>
<p dir="auto">On a related note (perhaps worthy of its own JIRA issue), given the following application context configuration and JUnit test class...</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xmlns:p=&quot;http://www.springframework.org/schema/p&quot;
  xmlns:util=&quot;http://www.springframework.org/schema/util&quot;
  xsi:schemaLocation=&quot;
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd&quot;&gt;

  &lt;bean id=&quot;listHolderParent&quot; class=&quot;com.example.MergedListTests$ListHolder&quot;
    abstract=&quot;true&quot;&gt;
    &lt;property name=&quot;list&quot;&gt;
      &lt;list&gt;
        &lt;value&gt;red&lt;/value&gt;
        &lt;value&gt;green&lt;/value&gt;
        &lt;value&gt;blue&lt;/value&gt;
      &lt;/list&gt;
    &lt;/property&gt;
  &lt;/bean&gt;

  &lt;bean id=&quot;listHolder&quot; parent=&quot;listHolderParent&quot;&gt;
    &lt;property name=&quot;list&quot;&gt;
      &lt;list merge=&quot;true&quot;&gt;
        &lt;value&gt;cyan&lt;/value&gt;
        &lt;value&gt;magenta&lt;/value&gt;
        &lt;value&gt;yellow&lt;/value&gt;
        &lt;value&gt;black&lt;/value&gt;
      &lt;/list&gt;
    &lt;/property&gt;
  &lt;/bean&gt;

&lt;/beans&gt;"><pre class="notranslate"><code class="notranslate">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd"&gt;

  &lt;bean id="listHolderParent" class="com.example.MergedListTests$ListHolder"
    abstract="true"&gt;
    &lt;property name="list"&gt;
      &lt;list&gt;
        &lt;value&gt;red&lt;/value&gt;
        &lt;value&gt;green&lt;/value&gt;
        &lt;value&gt;blue&lt;/value&gt;
      &lt;/list&gt;
    &lt;/property&gt;
  &lt;/bean&gt;

  &lt;bean id="listHolder" parent="listHolderParent"&gt;
    &lt;property name="list"&gt;
      &lt;list merge="true"&gt;
        &lt;value&gt;cyan&lt;/value&gt;
        &lt;value&gt;magenta&lt;/value&gt;
        &lt;value&gt;yellow&lt;/value&gt;
        &lt;value&gt;black&lt;/value&gt;
      &lt;/list&gt;
    &lt;/property&gt;
  &lt;/bean&gt;

&lt;/beans&gt;
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="package com.example;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MergedListTests {

  @Test
  public void mergedList() {
    ApplicationContext appCtx = new ClassPathXmlApplicationContext(
      &quot;/com/example/MergedListTests-context.xml&quot;);
    ListHolder listHolder = appCtx.getBean(&quot;listHolder&quot;, ListHolder.class);
    assertNotNull(listHolder);
    assertEquals(7, listHolder.getList().size());
  }

  public static class ListHolder {

    private List&lt;String&gt; list;

    public List&lt;String&gt; getList() {
      return this.list;
    }

    public void setList(List&lt;String&gt; list) {
      this.list = list;
    }
  }
}"><pre class="notranslate"><code class="notranslate">package com.example;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MergedListTests {

  @Test
  public void mergedList() {
    ApplicationContext appCtx = new ClassPathXmlApplicationContext(
      "/com/example/MergedListTests-context.xml");
    ListHolder listHolder = appCtx.getBean("listHolder", ListHolder.class);
    assertNotNull(listHolder);
    assertEquals(7, listHolder.getList().size());
  }

  public static class ListHolder {

    private List&lt;String&gt; list;

    public List&lt;String&gt; getList() {
      return this.list;
    }

    public void setList(List&lt;String&gt; list) {
      this.list = list;
    }
  }
}
</code></pre></div>
<p dir="auto">... changing the <strong></strong> definition in <em>listHolderParent</em> to <strong>util:list</strong> results in the following exception:</p>
<blockquote>
<p dir="auto">java.lang.IllegalArgumentException: Cannot merge with object of type [class org.springframework.beans.factory.config.BeanDefinitionHolder]<br>
at org.springframework.beans.factory.support.ManagedList.merge(ManagedList.java:98)<br>
at org.springframework.beans.factory.support.ManagedList.merge(ManagedList.java:1)<br>
at org.springframework.beans.MutablePropertyValues.mergeIfRequired(MutablePropertyValues.java:221)<br>
at org.springframework.beans.MutablePropertyValues.addPropertyValue(MutablePropertyValues.java:169)<br>
at org.springframework.beans.MutablePropertyValues.addPropertyValues(MutablePropertyValues.java:138)<br>
at org.springframework.beans.factory.support.AbstractBeanDefinition.overrideFrom(AbstractBeanDefinition.java:300)<br>
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:1114)<br>
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:1054)<br>
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1040)<br>
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:294)<br>
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:578)<br>
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398)<br>
at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139)<br>
at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:83)<br>
at com.example.MergedListTests.mergedList(MergedListTests.java:17)<br>
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br>
...</p>
</blockquote>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398096007" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10561" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10561/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10561">#10561</a> Merge multiple lists/maps/sets that share the same id  into a single map/list (<em><strong>"Cloned from"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398089583" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9658" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9658/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9658">#9658</a> support cascading merge operation (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398115480" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13477" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13477/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13477">#13477</a> Add MapCombiningFactoryBean (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398073321" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/7575" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/7575/hovercard" href="https://github.com/spring-projects/spring-framework/issues/7575">#7575</a> Support easier merge/concatenation of lists (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398151529" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14186" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14186/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14186">#14186</a> Respect 'merge' attribute of  and  elements in conjunction with nested property paths</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398087871" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9437" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9437/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9437">#9437</a> Merge of util:map does not work</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398061751" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/6201" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/6201/hovercard" href="https://github.com/spring-projects/spring-framework/issues/6201">#6201</a> Support for Collection Merging in Parent/Child Bean Definitions</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398091257" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9872" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9872/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9872">#9872</a> Re-allow merge of util:map list, set, ... based on the same bean ID</li>
</ul>
<p dir="auto"><strong>Referenced from:</strong> commits <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/a827ab88b160acdcb46c2d21c5dabb4b41dd57c5/hovercard" href="https://github.com/spring-projects/spring-framework/commit/a827ab88b160acdcb46c2d21c5dabb4b41dd57c5"><tt>a827ab8</tt></a></p>
<p dir="auto">16 votes, 18 watchers</p>