Skip to content

Support easier merge/concatenation of lists [SPR-2887] #7575

Closed

Description

@spring-projects-issues

Jasper Rosenberg opened SPR-2887 and commented

This is in regards to forum thread: http://forum.springframework.org/showthread.php?t=31932

Basically, I was looking for a way to factor out some common values of two lists. I was able to solve it ultimately by declaring the common list as a org.springframework.beans.factory.config.ListFactoryBean, and then having the shared lists declare the common list as a parent with the "sourceList" property of the child set to merge. This works but isn't very friendly for an operation that doesn't seem like it should be infrequent. One proposed solution in the forum thread was to add support for "abstract" and "parent" attributes to util:list. That would allow my current solution to at least be a bit terser.

Unfortunately, I don't think this really covers the general case very well, since what if someone wanted to merge in more than just one list? The bean inheritence model doesn't support multiple parents, so you would be back to square one...

org.springframework.beans.factory.config.PropertiesFactoryBean handles this pretty nicely with its support for the "propertiesArray" property.

I suppose whatever the solution ultimately is should also be applied to Maps and Sets if applicable.


Affects: 2.0.1

Attachments:

Issue Links:

16 votes, 18 watchers

Activity

  1. spring-projects-issues commented on Nov 1, 2007

    @spring-projects-issues
    CollaboratorAuthor

    allnightlong commented

    May be it would be better to add <subList> property?

    For example:

    <util:list id="list1">
    <value>a</value>
    <value>b</value>
    <value>c</value>
    </util:list>

    <util:list id="list2">
    <value>d</value>
    <value>e</value>
    <value>f</value>
    </util:list>

    <util:list id="mergedList">
    <subList ref="list1"/>
    <subList ref="list2"/>
    <value>g</value>
    <value>h</value>
    </util:list>

    The result of mergedList is: a,b,c,d,e,f,g,h

  2. spring-projects-issues commented on Feb 12, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Chuck Canning commented

    It would be really great if you could forward reference the list of properties. An example would be where in a lower level framework layer, you would define your standard database beans. Say we are using hibernate, so I have something like:

    <util:list id="myAnnotatedClasses" />
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <property name="annotatedClasses" ref="myAnnotatedClasses"/>
    </bean>
    

    Then at multiple layers through the system, you could add classes to the session factory by doing something like

    <utli:list parent="myAnnotatedClasses" merge="true"> <!-- or even append -->
        <value>Foo.class</value>
        <value>Bar.class</value>
    </util:list>
    

    and someone else could do

    <utli:list parent="myAnnotatedClasses" merge="true"> <!-- or even append -->
        <value>Ernie.class</value>
        <value>Bert.class</value>
    </util:list>
    

    in the end, the SessionFactory gets configured with
    <list>
    <value>Foo.class</value>
    <value>Bar.class</value>
    <value>Ernie.class</value>
    <value>Bert.class</value>
    </list>

    We have a lot of places in our system configuration where something like this would be useful. Now we are forced to configure all of this stuff at the highest level and some of the lists can become unwieldy since they can contain 1000's of items. This would allow us to break the lists into multiple configurations.

  3. spring-projects-issues commented on May 8, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Doug Bateman commented

    I also would have a use for this feature. However, I'd want to make sure that not only is it supported by the util:list syntax (which I do like), but also in the ListFActoryBean itself (similar to PropertiesFactoryBean)... like this...

    <bean id="mergedList" class="org.springframework.beans.factory.config.ListFactoryBean" >
    	<property name="sourceListArray">
    		<list>
    			<ref local="sublistOne"/>
    			<ref local="sublistTwo"/>
    		</list>
    	</property>
    </bean>
    
    <bean id="sublistOne" class="org.springframework.beans.factory.config.ListFactoryBean" >
    	<property name="sourceList">
    		<list>
    			<value>A</value>
    			<value>B</value>
    		</list>
    	</property>
    </bean>
    
    <bean id="sublistTwo" class="org.springframework.beans.factory.config.ListFactoryBean" >
    	<property name="sourceList">
    		<list>
    			<value>A</value>
    			<value>B</value>
    		</list>
    	</property>
    </bean>
    
  4. spring-projects-issues commented on May 10, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Brad Davis commented

    I'm looking at an approach to this that would resolve this in a slightly different way. Given the below XML (disregarding the new unroll-collections attribute) you end up with a list of lists each containing one item...

    <util:list id="coreClasses">
        <value>foo</value>
    </util:list>
    
    <util:list id="musicClasses">
        <value>bar</value>
    </util:list>
    
    <util:list id="vizmediaClasses" >
        <value>baz</value>
    </util:list>
    
    <util:list id="swiftClasses" unroll-collections="true">
        <ref bean="coreClasses" />
        <ref bean="musicClasses" />
        <ref bean="vizmediaClasses" />
    </util:list>
    

    However, I made a modification to the UtilNamespaceHandler and ListFactoryBeans class that causes the unroll-collections attribute to trigger in the factory the ability to recognize when a member of the sourceList is a collection itself and if so, to add the members of that collection to the resulting list bean.

    There are some caveats. It doesn't work well for a collection that is intended to contain other collections. I'm unsure how to integrate it with the TypeConverter code in the ListFactoryBean, and I can't seem to find find generalized utility code for converting a string to a boolean value (there's duplicated code in CustomBooleanEditor, PortletRequestUtils and ServletRequestUtils).

    I'm attaching a first pass patch.

  5. spring-projects-issues commented on Sep 17, 2008

    @spring-projects-issues
    CollaboratorAuthor

    Chuck Canning commented

    I have posted another solution that allows you to merge any number of child lists into a parent list by merging the values instead of have to specify each list. The code and simple test example is here: http://forum.springframework.org/showthread.php?p=202207#post202207. It would be very easy to post from mine solution into the current codebase if so desired.

  6. spring-projects-issues commented on Apr 14, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Nathan Deckard commented

    This thread helped me work around the problem of combining hibernate mappings from multiple spring contexts into 1 list: http://forum.springsource.org/showthread.php?t=18135.

  7. spring-projects-issues commented on Sep 8, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Brad Davis commented

    I've since worked around this issue with the attached classes. I have the same problem as previously mentioned, how to combine lists of classes in different projects into a single list to be passed to a hibernate configuration. Its not as versatile as being able to pull other lists inline into the one you're creating, but it does the trick.

  8. spring-projects-issues commented on Nov 5, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Alessio Stalla commented

    FWIW, I solved this problem in my project by patching ListFactoryBean (and SetFactoryBean as well) to accept multiple source lists. You can see the source for it here: http://code.google.com/p/dynaspring/source/browse/trunk/src/dynaspring/support/ListFactoryBean.java. Syntax-wise, my project does not use XML (it's a DSL meant to replace XML in Spring!), so I don't have a ready solution for that; the merge of two lists is accomplished in two different (and orthogonal) ways: by having a master list that merges several children lists (case 1), and by having several peer lists with the same name and the "merge" property set to true (case 2). Translated to XML, it might look like:

    case 1)

    <util:list id="foo">
    <list><value>1</value></list>
    <value>2</value>
    <ref bean="otherList" />
    </util:list>

    <!-- foo will be [1, 2, ...the elements of otherList...] - this is semantically different from what Spring does with util:list now! -->

    case 2)

    <util:list id="foo" merge="true">
    <value>1</value>
    </util:list>

    <util:list id="foo" merge="true">
    <value>2</value>
    </util:list>

    <!-- foo will be [1, 2] - this is not supported by Spring now, but it's not semantically incompatible -->In my project, the effect of merge=true is simply to add beanFactory.getBeanDefinition(nameOfTheList) to the source lists of the ListFactoryBean. I don't know very well how custom namespaces work in Spring, but if they can do that, then case 2) could be ported without much hassle.

  9. spring-projects-issues commented on Apr 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Duplicates #11189

  10. spring-projects-issues commented on Apr 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Duplicates #11189

  11. added
    type: enhancement
    in: core
    has: votes-jira
    status: duplicate
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    has: votes-jirain: corestatus: duplicatetype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions