Spring ListFactoryBean, SetFactoryBean and MapFactoryBean Example
This is an example of how ListFactoryBean
, SetFactoryBean
and MapFactoryBean
classes can be used to instantiate Lists, Sets and Maps and inject them as properties in a Spring Bean. The three classes provided by the Spring API are simple factories for shared List
, Set
and Map
instances and allow for central setup of Lists, Sets and Maps via the <list>
, <set>
and <map>
elements in XML bean definitions.
In order to show how these classes can be used to instantiate Collections in a Spring Bean we will create a simple Spring Bean with three properties, List
, Set
and Map
and add the neccesary configuration in XML definitions.
Our preferred development environment is Eclipse. We are using Eclipse Juno (4.2) version, along with Maven Integration plugin version 3.1.0. You can download Eclipse from here and Maven Plugin for Eclipse from here. The installation of Maven plugin for Eclipse is out of the scope of this tutorial and will not be discussed. We are also using Spring version 3.2.3 and the JDK 7_u_21.
Let’s begin.
1. Create a new Maven project
Go to File -> Project ->Maven -> Maven Project.
In the “Select project name and location” page of the wizard, make sure that “Create a simple project (skip archetype selection)” option is checked, hit “Next” to continue with default values.
In the “Enter an artifact id” page of the wizard, you can define the name and main package of your project. We will set the “Group Id” variable to "com.javacodegeeks.snippets.enterprise"
and the “Artifact Id” variable to "springexample"
. The aforementioned selections compose the main project package as "com.javacodegeeks.snippets.enterprise.springexample"
and the project name as "springexample"
. Hit “Finish” to exit the wizard and to create your project.
The Maven project structure is shown below:
- It consists of the following folders:
- /src/main/java folder, that contains source files for the dynamic content of the application,
- /src/test/java folder contains all source files for unit tests,
- /src/main/resources folder contains configurations files,
- /target folder contains the compiled and packaged deliverables,
- the pom.xml is the project object model (POM) file. The single file that contains all project related configuration.
2. Add Spring 3.2.3 dependency
- Locate the “Properties” section at the “Overview” page of the POM editor and perform the following changes:
Create a new property with name org.springframework.version and value 3.2.3.RELEASE. - Navigate to the “Dependencies” page of the POM editor and create the following dependencies (you should fill the “GroupId”, “Artifact Id” and “Version” fields of the “Dependency Details” section at that page):
Group Id : org.springframework Artifact Id : spring-web Version : ${org.springframework.version}
Alternatively, you can add the Spring dependencies in Maven’s pom.xml
file, by directly editing it at the “Pom.xml” page of the POM editor, as shown below:
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.javacodegeeks.snippets.enterprise</groupId> <artifactId>springexample</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> </dependencies> <properties> <spring.version>3.2.3.RELEASE</spring.version> </properties> </project>
As you can see Maven manages library dependencies declaratively. A local repository is created (by default under {user_home}/.m2 folder) and all required libraries are downloaded and placed there from public repositories. Furthermore intra – library dependencies are automatically resolved and manipulated.
3. Create a simple Spring Bean with Collections properties
We create a simple Spring Bean, HelloWorld
, that has three properties, a Map
, a Set
and a List
field.
HelloWorld.java:
package com.javacodegeeks.snippets.enterprise.services; import java.util.List; import java.util.Map; import java.util.Set; @SuppressWarnings("rawtypes") public class HelloWorld { private List list; private Set set; private Map map; public List getList() { return list; } public void setList(List list) { this.list = list; } public Set getSet() { return set; } public void setSet(Set set) { this.set = set; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } }
4. XML-based approach for Collections injection into Spring Bean properties, using the Spring API factory Classes
In order to add a value that belongs to one of the Spring supported Collections, we can use the <list/>
, <set/>
and <map/>
elements inside the <property/>
element. There we can define the factory beans that we will use in order to instantiate the Collections.
Note that, the definition of the ListFactoryBean
class needs the sourceList
that contains the values to be put in the list, and the targetListClass
that is the implementation of the List that will be used. The same definition is needed in the SetFactoryBean
and the MapFactoryBean
.
ListFactoryBean example
<property name="list"> <bean class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="targetListClass"> <value>java.util.ArrayList</value> </property> <property name="sourceList"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> </bean> </property>
SetFactoryBean example
<property name="set"> <bean class="org.springframework.beans.factory.config.SetFactoryBean"> <property name="targetSetClass"> <value>java.util.HashSet</value> </property> <property name="sourceSet"> <set> <value>1</value> <value>2</value> <value>3</value> </set> </property> </bean> </property>
MapFactoryBean example
<property name="map"> <bean class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="targetMapClass"> <value>java.util.HashMap</value> </property> <property name="sourceMap"> <map> <entry key="Key1" value="1" /> <entry key="Key2" value="2" /> <entry key="Key3" value="3" /> </map> </property> </bean> </property>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <bean id="helloWorldBean" class="com.javacodegeeks.snippets.enterprise.services.HelloWorld"> <property name="list"> <bean class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="targetListClass"> <value>java.util.ArrayList</value> </property> <property name="sourceList"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> </bean> </property> <property name="set"> <bean class="org.springframework.beans.factory.config.SetFactoryBean"> <property name="targetSetClass"> <value>java.util.HashSet</value> </property> <property name="sourceSet"> <set> <value>1</value> <value>2</value> <value>3</value> </set> </property> </bean> </property> <property name="map"> <bean class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="targetMapClass"> <value>java.util.HashMap</value> </property> <property name="sourceMap"> <map> <entry key="Key1" value="1" /> <entry key="Key2" value="2" /> <entry key="Key3" value="3" /> </map> </property> </bean> </property> </bean> </beans>
Alternatively, we can include the util schema
and add the <util:list>
and have the same result.
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="helloWorldBean" class="com.javacodegeeks.snippets.enterprise.services.HelloWorld"> <property name="list"> <util:list list-class="java.util.ArrayList"> <value>1</value> <value>2</value> <value>3</value> </util:list> </property> <property name="set"> <util:set set-class="java.util.HashSet"> <value>1</value> <value>2</value> <value>3</value> </util:set> </property> <property name="map"> <util:map map-class="java.util.HashMap"> <entry key="Key1" value="1" /> <entry key="Key2" value="2" /> <entry key="Key3" value="3" /> </util:map> </property> </bean> </beans>
4. Run the application
In the App.class
we load the helloWorldBean
through the ApplicationContext
, and use the fields’ getters to get the values set to the collections in applicationContext.xml
.
App.java:
package com.javacodegeeks.snippets.enterprise; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.javacodegeeks.snippets.enterprise.services.HelloWorld; public class App { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld hello = (HelloWorld) context.getBean("helloWorldBean"); System.out.println("List: " + hello.getList()); System.out.println("Set : " + hello.getSet()); System.out.println("Map : " + hello.getMap()); } }
5. Output
When we execute the application, the elements contained in each collection are returned :
List: [1, 2, 3]
Set : [3, 2, 1]
Map : {Key2=2, Key1=1, Key3=3}
This was an example of ListFactoryBean
, SetFactoryBean
and MapFactoryBean
use for Collections injection in Spring beans.
Download the Eclipse project of this tutorial : SpringCollectionsFactoryBeansExample.zip