A List
object is something we often rely on in our classes and there are times when we may need to configure it directly in our bean configurations.spring list
In this article we will see several examples of list
element. We will create an empty list, a proper list, a list with a mix of different value types and finally a list which enforces the value type.
We will also see how to create a list object using ListFactoryBean
and util:list
.
So let’s get start with the examples but first a bit about the setup.
Dependencies
Add the following dependencies:
spring-core
spring-context
spring-beans
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javarticles.spring</groupId> <artifactId>springListExample</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <properties> <spring.version>3.2.3.RELEASE</spring.version> </properties> </project>
About the List Examples
Our examples contain two central beans Tutorial
and Topic
. A tutorial is composed of list of topics. It also has a name
attribute to describe the tutorial.
Topic:
package com.javarticles.spring; public class Topic { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return name; } }
Tutorial:
package com.javarticles.spring; import java.util.ArrayList; import java.util.List; public class Tutorial { private String name; private List<?> topicsList = new ArrayList<>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public List<?> getTopicsList() { return topicsList; } public void setTopicsList(List<?> topicsList) { this.topicsList = topicsList; } public String toString() { return name + topicsList; } }
ListFactoryBean Example
In this example, we will use spring provided ListFactoryBean
, a FactoryBean
implementation, to create a java.util.List
instance initialized with values provided by sourceList
property. The targetListClass
property defines the actual implementation type of the List
. Once we have defined out list factory, we just need to point the list property topicsList
to the bean reference topicListFactory
.
listFactoryApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="topicListUsingFactory" class="com.javarticles.spring.Tutorial"> <property name="name" value="Get Topics using ListFactoryBean"></property> <property name="topicsList" ref="topicListFactory" /> </bean> <bean id="topicListFactory" class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="sourceList"> <list> <ref local="javaCore" /> <ref local="scalaBasics" /> </list> </property> <property name="targetListClass"> <value>java.util.LinkedList</value> </property> </bean> <bean id="javaCore" class="com.javarticles.spring.Topic"> <property name="name" value="JavaCore" /> </bean> <bean id="scalaBasics" class="com.javarticles.spring.Topic"> <property name="name" value="ScalaBasics" /> </bean> </beans>
SpringListFactoryBeanExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringListFactoryBeanExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "listFactoryApplicationContext.xml"); try { Tutorial tutorial = (Tutorial) context.getBean("topicListUsingFactory"); System.out.println(tutorial); } finally { context.close(); } } }
Output:
Get Topics using ListFactoryBean[JavaCore, ScalaBasics]
util:list Example
Instead of explicitly relying on ListFactoryBean
, you can use the spring utility schema which has an easy way of creating list using <util:list>
element. You can define the exact type of List using the list-class
attribute on the element. Let’s do the above example using util:list
.
listFactoryApplicationContext.xml:
<?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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd"> <bean id="topicListUsingFactory" class="com.javarticles.spring.Tutorial"> <property name="name" value="Get Topics using ListFactoryBean"></property> <property name="topicsList" ref="topicListFactory" /> </bean> <bean id="topicListFactory" class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="sourceList"> <list> <ref local="javaCore" /> <ref local="scalaBasics" /> </list> </property> <property name="targetListClass"> <value>java.util.LinkedList</value> </property> </bean> <bean id="topicListUsingUtilList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Get Topics using Util:List"></property> <property name="topicsList" ref="topicListUtil" /> </bean> <util:list id="topicListUtil" list-class="java.util.LinkedList"> <ref local="javaCore" /> <ref local="scalaBasics" /> </util:list> <bean id="javaCore" class="com.javarticles.spring.Topic"> <property name="name" value="JavaCore" /> </bean> <bean id="scalaBasics" class="com.javarticles.spring.Topic"> <property name="name" value="ScalaBasics" /> </bean> </beans>
SpringListFactoryBeanExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringListFactoryBeanExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "listFactoryApplicationContext.xml"); try { Tutorial tutorial = (Tutorial) context.getBean("topicListUsingFactory"); System.out.println(tutorial); tutorial = (Tutorial) context.getBean("topicListUsingUtilList"); System.out.println(tutorial); } finally { context.close(); } } }
Output:
Get Topics using ListFactoryBean[JavaCore, ScalaBasics] Get Topics using Util:List[JavaCore, ScalaBasics]
Set Empty List
We can also directly create the list using collection element <list>
. Let’s start with an empty list. You just need to include empty <list>
element without any values.
emptyListApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="emptyList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Empty List"/> <property name="topicsList"> <list> </list> </property> </bean> </beans>
SpringEmptyListExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringEmptyListExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "emptyListApplicationContext.xml"); try { Tutorial tutorial = (Tutorial) context.getBean("emptyList"); System.out.println(tutorial); } finally { context.close(); } } }
Output:
Empty List[]
Inject List Using Spring list Element “spring list”
Let’s inject a proper list using spring’s <list>
element. The list
element contains two values which refer to Topic
beans javaCore
and scalaBasics
.
listApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="onlyTopicList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Proper List Example"/> <property name="topicsList"> <list> <ref local="javaCore" /> <ref local="scalaBasics" /> </list> </property> </bean> <bean id="javaCore" class="com.javarticles.spring.Topic"> <property name="name" value="JavaCore" /> </bean> <bean id="scalaBasics" class="com.javarticles.spring.Topic"> <property name="name" value="ScalaBasics" /> </bean> </beans>
SpringListExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringListExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "listApplicationContext.xml"); try { Tutorial tutorial = (Tutorial) context.getBean("onlyTopicList"); System.out.println(tutorial); } finally { context.close(); } } }
Output:
Proper List Example[JavaCore, ScalaBasics]
List With Mixed Value Types
In this example, we will create a list of which contains a mix of references and individual values.
Our bean mixedTypeTopicList
contains Topic
bean, a string value, a string value which is a bean ID and a list containing another set of mixed values.
listApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="onlyTopicList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Proper List Example"/> <property name="topicsList"> <list> <ref local="javaCore" /> <ref local="scalaBasics" /> </list> </property> </bean> <bean id="javaCore" class="com.javarticles.spring.Topic"> <property name="name" value="JavaCore" /> </bean> <bean id="scalaBasics" class="com.javarticles.spring.Topic"> <property name="name" value="ScalaBasics" /> </bean> <bean id="mixedTypeTopicList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Mixed List Example"/> <property name="topicsList"> <list> <ref local="javaCore" /> <value>Concurrency</value> <idref local="scalaBasics" /> <list> <ref local="android" /> <idref local="spring" /> </list> </list> </property> </bean> <bean id="spring" class="com.javarticles.spring.Topic"> <property name="name" value="Spring" /> </bean> <bean id="android" class="com.javarticles.spring.Topic"> <property name="name" value="Android" /> </bean> </beans>
SpringListExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringListExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "listApplicationContext.xml"); try { Tutorial tutorial = (Tutorial) context.getBean("onlyTopicList"); System.out.println(tutorial); tutorial = (Tutorial) context.getBean("mixedTypeTopicList"); System.out.println(tutorial); } finally { context.close(); } } }
Output:
Proper List Example[JavaCore, ScalaBasics] Mixed List Example[JavaCore, Concurrency, scalaBasics, [Android, spring]]
Enforce List Value Type
We can also enforce list’s value type using value-type
attribute. We have added a new bean topicTypeList
to our configuration, the value-type</code. attribute is set to
com.javarticles.spring.Topic
which means the list will accept only values of type Topic
.
listApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="onlyTopicList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Proper List Example"/> <property name="topicsList"> <list> <ref local="javaCore" /> <ref local="scalaBasics" /> </list> </property> </bean> <bean id="javaCore" class="com.javarticles.spring.Topic"> <property name="name" value="JavaCore" /> </bean> <bean id="scalaBasics" class="com.javarticles.spring.Topic"> <property name="name" value="ScalaBasics" /> </bean> <bean id="mixedTypeTopicList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Mixed List Example"/> <property name="topicsList"> <list> <ref local="javaCore" /> <value>Concurrency</value> <idref local="scalaBasics" /> <list> <ref local="android" /> <idref local="spring" /> </list> </list> </property> </bean> <bean id="spring" class="com.javarticles.spring.Topic"> <property name="name" value="Spring" /> </bean> <bean id="android" class="com.javarticles.spring.Topic"> <property name="name" value="Android" /> </bean> <bean id="topicTypeList" class="com.javarticles.spring.Tutorial"> <property name="name" value="Enforce value-type List Example"/> <property name="topicsList"> <list value-type="com.javarticles.spring.Topic"> <ref local="javaCore" /> <ref local="scalaBasics" /> </list> </property> </bean> </beans>
SpringListExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringListExample { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "listApplicationContext.xml"); try { Tutorial tutorial = (Tutorial) context.getBean("onlyTopicList"); System.out.println(tutorial); tutorial = (Tutorial) context.getBean("mixedTypeTopicList"); System.out.println(tutorial); tutorial = (Tutorial) context.getBean("topicTypeList"); System.out.println(tutorial); } finally { context.close(); } } }
Output:
Proper List Example[JavaCore, ScalaBasics] Mixed List Example[JavaCore, Concurrency, scalaBasics, [Android, spring]] Enforce value-type List Example[JavaCore, ScalaBasics]
Download the source code “spring list”
This was an example about spring list element and spring’s ListFactoryBean. You can download the source code here: springListExample.zip