如何在 spring 配置中扩展列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6464506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to extend a list in spring config
提问by Bastl
I have defined a with some "common" values. How can I extend the common list by additional values to various new beans?
我已经用一些“通用”值定义了 a 。如何通过对各种新 bean 的附加值来扩展公共列表?
<util:list id="myCommonList" list-class="java.util.LinkedList">
<bean .../>
<bean .../>
<bean .../>
<bean .../>
</util:list>
<bean id="extension" parent="myCommonList">
<bean ref="additionalValue"/>
</bean>
Will this overwrite the list or extend it?
这会覆盖列表还是扩展它?
回答by skaffman
You can do it, but not using <util:list>, which is just a convenience syntax. The container does provide a "collection merging" function, but you have to use the "old" style:
您可以这样做,但不能使用<util:list>,这只是一种方便的语法。容器确实提供了“集合合并”功能,但您必须使用“旧”样式:
<bean id="parent" class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list>
<value>X</value>
</list>
</property>
</bean>
<bean id="child" parent="parent" class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list merge="true">
<value>Y</value>
</list>
</property>
</bean>
回答by Bird Bird
Based on skaffman's answer, you can achive this way:
根据 skaffman 的回答,您可以通过以下方式实现:
<util:list id="parent">
<value>X</value>
</util:list>
<bean id="child" parent="parent" class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="sourceList">
<list merge="true">
<value>Y</value>
</list>
</property>
</bean>
回答by abalogh
回答by Mikhail Tsaplin
Today I was having this issue too. For me the acceptable solution was to use SpEL and while SpEL doesn't supports multiple statements - create two auxiliary classes that appends lists.
今天我也遇到了这个问题。对我来说,可接受的解决方案是使用 SpEL,虽然 SpEL 不支持多个语句 - 创建两个附加列表的辅助类。
Context might be implemented like this:
上下文可能是这样实现的:
<util:list id="list1">
<value>str1</value>
<value>str2</value>
</util:list>
<util:list id="list2">
<value>str3</value>
<value>str4</value>
</util:list>
<bean ...>
<property name="propertyThatRequiresMergedList"
value="#{ new x.y.springs.StringListsMerger().merge(@list1, @list2) }" />
</bean>
And classes:
和类:
package x.y.springs;
import java.util.List;
public abstract class ListsMerger<T extends List> {
public T merge(T ... lists) {
T container = createContainer();
for (T list : lists) {
container.addAll(list);
}
return container;
}
public abstract T createContainer();
}
package x.y.springs;
import java.util.ArrayList;
import java.util.List;
public class StringListsMerger extends ListsMerger<List<String>> {
@Override
public List<String> createContainer() {
return new ArrayList<String>();
}
}
回答by adrock20
To append list2's elements to list1 use the following spring config.
要将 list2 的元素附加到 list1,请使用以下 spring 配置。
<util:list id="list1">
<value>1</value>
<util:list>
<util:list id="list2">
<value>3</value>
<value>5</value>
<util:list>
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="list1" />
<property name="targetMethod" value="addAll" />
<property name="arguments" ref="list2" />
</bean>
回答by Nani
You can overwrite or add any other bean in this list by following the below snippet.
您可以按照以下代码段覆盖或添加此列表中的任何其他 bean。
For example: this is your bean which needs to be injected in the AAA util:list mentioned below.
例如:这是您的 bean,需要注入到下面提到的 AAA util:list 中。
<bean id = "BBB" class=""/>
<util:list id="AAA"/>
<bean id="AAAListMergeDirective" depends-on="AAA"
parent="listMergeDirective" >
<property name="add" ref="BBB" />
</bean>

