Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-6523

Introduce 'merge' attribute for util namespace collection elements

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: Pending Closure
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      Section C.2.2.4 <util:list/> of the reference manual states:

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

      This is, however, not true. In contrast to the standard support for collections (i.e., array, list, set, map) in the beans namespace, the util namespace does not support the merge attribute for collections.

      Is this an oversight in the documentation or missing functionality in the util namespace support?

      On a related note (perhaps worthy of its own JIRA issue), given the following application context configuration and JUnit test class...

      <?xml version="1.0" encoding="UTF-8"?>
      <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">
       
        <bean id="listHolderParent" class="com.example.MergedListTests$ListHolder"
          abstract="true">
          <property name="list">
            <list>
              <value>red</value>
              <value>green</value>
              <value>blue</value>
            </list>
          </property>
        </bean>
       
        <bean id="listHolder" parent="listHolderParent">
          <property name="list">
            <list merge="true">
              <value>cyan</value>
              <value>magenta</value>
              <value>yellow</value>
              <value>black</value>
            </list>
          </property>
        </bean>
       
      </beans>
      

      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<String> list;
       
          public List<String> getList() {
            return this.list;
          }
       
          public void setList(List<String> list) {
            this.list = list;
          }
        }
      }
      

      ... changing the <list> definition in listHolderParent to <util:list> results in the following exception:

      java.lang.IllegalArgumentException: Cannot merge with object of type [class org.springframework.beans.factory.config.BeanDefinitionHolder]
      at org.springframework.beans.factory.support.ManagedList.merge(ManagedList.java:98)
      at org.springframework.beans.factory.support.ManagedList.merge(ManagedList.java:1)
      at org.springframework.beans.MutablePropertyValues.mergeIfRequired(MutablePropertyValues.java:221)
      at org.springframework.beans.MutablePropertyValues.addPropertyValue(MutablePropertyValues.java:169)
      at org.springframework.beans.MutablePropertyValues.addPropertyValues(MutablePropertyValues.java:138)
      at org.springframework.beans.factory.support.AbstractBeanDefinition.overrideFrom(AbstractBeanDefinition.java:300)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:1114)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java:1054)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1040)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:294)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:578)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398)
      at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
      at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
      at com.example.MergedListTests.mergedList(MergedListTests.java:17)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      ...

        Issue Links

          Activity

          Hide
          sslavic Stevo Slavić added a comment -

          This issue is related to SPR-1502 and SPR-4760

          Show
          sslavic Stevo Slavić added a comment - This issue is related to SPR-1502 and SPR-4760
          Hide
          cbeams Chris Beams added a comment -

          As Juergen originally mentioned, the lack of merge support for collections has been a limitation of the util namespace from its inception. The real 'bug' here was with documentation, which Sam has since fixed. This is really an improvement request, and now marking it as such.

          Show
          cbeams Chris Beams added a comment - As Juergen originally mentioned, the lack of merge support for collections has been a limitation of the util namespace from its inception. The real 'bug' here was with documentation, which Sam has since fixed. This is really an improvement request, and now marking it as such.
          Hide
          erh Eric Haszlakiewicz added a comment -

          It sounds like this also needs a "parent" attribute like the regular bean element has, so you can specify what you're merging with. i.e.

           
              <util:map id="map1">
                  ...
              </util:map>
              <util:map id="map2" parent="map1" merge="true">
                  ...
              </util:map>

          Show
          erh Eric Haszlakiewicz added a comment - It sounds like this also needs a "parent" attribute like the regular bean element has, so you can specify what you're merging with. i.e. < util :map id = "map1" > ... </ util :map> < util :map id = "map2" parent = "map1" merge = "true" > ... </ util :map>
          Hide
          matthewadams Matthew T. Adams added a comment -

          One thing that I'd like to make sure of in this request is that the ordering be controllable, in particular with maps. I'm commenting here as my issue, SPR-8835, "Add MapCombiningFactoryBean", was resolved as a duplicate & Chris Beams just wanted to make sure that I got my $0.02 in WRT ordering. See https://jira.springsource.org/secure/attachment/19107/MapCombiningFactoryBean.java for more information.

          Show
          matthewadams Matthew T. Adams added a comment - One thing that I'd like to make sure of in this request is that the ordering be controllable, in particular with maps. I'm commenting here as my issue, SPR-8835 , "Add MapCombiningFactoryBean", was resolved as a duplicate & Chris Beams just wanted to make sure that I got my $0.02 in WRT ordering. See https://jira.springsource.org/secure/attachment/19107/MapCombiningFactoryBean.java for more information.
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          At this point, I'm inclined to leave things as-is with respect to XML collection merging, since it only matters in combination with a feature of decreasing popularity: parent bean definitions. If there's a strong need for this appear in our XML configuration story in 2016 still, feel free to comment on this issue. If we don't hear about strong use cases in time for 4.3, we'll close the ticket for good.

          Juergen

          Show
          juergen.hoeller Juergen Hoeller added a comment - At this point, I'm inclined to leave things as-is with respect to XML collection merging, since it only matters in combination with a feature of decreasing popularity: parent bean definitions. If there's a strong need for this appear in our XML configuration story in 2016 still, feel free to comment on this issue. If we don't hear about strong use cases in time for 4.3, we'll close the ticket for good. Juergen

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              sbrannen Sam Brannen
              Last updater:
              Juergen Hoeller
            • Votes:
              16 Vote for this issue
              Watchers:
              18 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                34 weeks, 4 days ago