xmlspringlistspring-iocdefault-implementation

Override default implementation of list or other collections in spring xml configuration file


I observed that the default implementation of list in spring xml is ArrayList.

I tried:

<bean id="employee" class="com.ioc.entity.Employee">
</property>
        <property name="list">
        <list value-type="java.lang.String">
        <value>ABC</value>
        <value>XYZ</value>
        </list>
        </property>
</bean>

getClass() method on this list returns java.util.ArrayList.

Is there any property or way by which we can override this default implementation of list (may be to LinkedList or any list that I want) or any other collection (like map, set etc.)?


Solution

  • You can import Spring util and use the list-class reference.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:util="http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                         http://www.springframework.org/schema/util
                         http://www.springframework.org/schema/util/spring-util-2.5.xsd">
    
    <bean id="employee" class="com.ioc.entity.Employee">
      <property name="list">
        <util:list id="myLinkedList" value-type="java.lang.String" list-class="java.util.LinkedList">
          <value>ABC</value>
          <value>XYZ</value>
        </util:list>
      </property>
    </bean>