Repository navigation
Description
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:
- AListAggregationFactoryBean.java (1.02 kB)
- ClassListAggregationFactoryBean.java (584 bytes)
- patch.txt (5.93 kB)
Issue Links:
- Introduce 'merge' attribute for util namespace collection elements [SPR-6523] #11189 Introduce 'merge' attribute for util namespace collection elements ("duplicates")
16 votes, 18 watchers
Activity
spring-projects-issues commentedon Nov 1, 2007
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
spring-projects-issues commentedon Feb 12, 2008
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:
Then at multiple layers through the system, you could add classes to the session factory by doing something like
and someone else could do
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.
spring-projects-issues commentedon May 8, 2008
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...
spring-projects-issues commentedon May 10, 2008
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...
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.
spring-projects-issues commentedon Sep 17, 2008
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.
spring-projects-issues commentedon Apr 14, 2009
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.
spring-projects-issues commentedon Sep 8, 2009
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.
spring-projects-issues commentedon Nov 5, 2010
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.
spring-projects-issues commentedon Apr 3, 2011
Chris Beams commented
Duplicates #11189
spring-projects-issues commentedon Apr 3, 2011
Chris Beams commented
Duplicates #11189