简体   繁体   中英

how does the <list> tag work in spring

I have a Collection instruments; in my SomeClass.java and I declare in my temp.xml file a bean of the class SomeClass.java. In the xml I add two string objects to the collection.

My question is Collection is an interface so I cannot instantiate it and List is also an interface so I don't think we can do

 Collection<String> someCollection = new List<String>();

I would like to know how the java code works when we use the list tag in the xml file. Meaning do the objects get stored in a linked list or arraylist or some type of list?

This depends on the ApplicationContext . Each implementation could be different, but you can be sure that the result is a List . Accoding documentation :

Another custom namespace utility is for creating lists. The first bean definition is identical to the ListFactoryBean example except it's a little shorter and easier to read. The second bean definition is the same except it uses the list-class attribute to specify what List implementation to use. When the list-class attribute isn't used, the ApplicationContext will choose the implementation class.

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

Checking the implementation of ListFactoryBean you could see that ArrayList is the default list implementation that is instantiated if no specific list type is provided. The piece of code that does this task is:

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

For , Spring IOC Container uses ArrayList class to make object. similarly for it uses LinkedHashSet and for it uses LinkedHashMap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM