Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 692
anonuser0428 Avatar asked Jul 09 '12 16:07

anonuser0428


1 Answers

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());
}
like image 127
Francisco Spaeth Avatar answered Oct 13 '22 21:10

Francisco Spaeth