<list>标签在春天如何工作?

ano*_*428 5 java xml collections spring dependency-injection

我有收藏乐器; 在我的SomeClass.java中,我在temp.xml文件中声明了SomeClass.java类的bean.在xml中,我将两个字符串对象添加到集合中.

我的问题是Collection是一个接口所以我无法实例化它而List也是一个接口所以我认为我们无法做到

 Collection<String> someCollection = new List<String>();
Run Code Online (Sandbox Code Playgroud)

我想知道当我们在xml文件中使用list标签时java代码是如何工作的.意味着对象是存储在链表或arraylist还是某种类型的列表中?

Fra*_*eth 11

这取决于ApplicationContext.每个实现可能会有所不同,但您可以确定结果是a List.编码文件:

另一个自定义命名空间实用程序用于创 第一个bean定义与ListFactoryBean示例相同,只是它更短,更容易阅读.第二个bean定义是相同的,除了它使用list-class属性来指定要使用的List实现.如果未使用list-class属性,ApplicationContext将选择实现类.

<util:list id="messageUtilList">
    <ref bean="stringMessage01"/>
    <ref bean="stringMessage02"/>
    <value>Spring is fun to learn.</value>
</util:list>

<util:list id="messageUtilLinkedList" 
           list-class="java.util.LinkedList">
    <ref bean="stringMessage01"/>
    <ref bean="stringMessage02"/>
    <value>Spring is fun to learn.</value>
</util:list>
Run Code Online (Sandbox Code Playgroud)

如果没有提供特定的列表类型,检查您的实现ListFactoryBean可能会看到这ArrayList是实例化的默认列表实现.执行此任务的代码片段是:

if (this.targetListClass != null) {
    result = (List) BeanUtils.instantiateClass(this.targetListClass);
}
else {
    result = new ArrayList(this.sourceList.size());
}
Run Code Online (Sandbox Code Playgroud)