Home » Spring » Injecting Collection in Spring Example

Injecting Collection in Spring Example

In this example we will show you how to inject Collections in Spring Beans properties. The types of Collections that are supported by Spring are List, Set, Map and Properties.

In this example we will create a simple Spring Bean with four properties, List, Set, Map and Properties.

Files required..

  • CustomerBean.java
  • PersonBean.java
  • applicationContext.xml
  • Test.java

Tools and Technologies used in this article :

  • Spring
  • JDK
  • Eclipse

1. Create a java project in Eclipse

Open Eclipse –> File –> New –> and Select Java project and click on next

Spring Hello World Setter Injection

Assign the name of the project as CollectionInjection and click Finish.

Spring Hello World Setter Injection


2. Add jar files to classpath of Project

Right click on the project and create a folder and name it to lib. After creation of lib folder and place following jar files into this folder and add these jars to class path of the project.

aopalliance-1.0.jar
commons-logging-1.1.1.jar
spring-aop-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-core-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar

Folder Structure

Complete folder structure of the project is given below.

Spring Collection Injection


Creating Beans

Now create a bean class into the package “com.jwt.spring” and create two bean files PersonBean and CustomerBean inside this package.

CustomerBean.java

package com.jwt.spring;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CustomerBean {
	private List<Object> lists;
	private Set<Object> sets;
	private Map<Object, Object> maps;
	private Properties pros;
	public List<Object> getLists() {
		return lists;
	}
	public void setLists(List<Object> lists) {
		this.lists = lists;
	}
	public Set<Object> getSets() {
		return sets;
	}
	public void setSets(Set<Object> sets) {
		this.sets = sets;
	}
	public Map<Object, Object> getMaps() {
		return maps;
	}
	public void setMaps(Map<Object, Object> maps) {
		this.maps = maps;
	}
	public Properties getPros() {
		return pros;
	}
	public void setPros(Properties pros) {
		this.pros = pros;
	}
	@Override
	public String toString() {
		return "Customer [lists=" + lists + ", sets=" + sets + ", maps=" + maps
				+ ", pros=" + pros + "]";
	}
} 
}

The above Listing 2 section creates a class CustomerBean and defines four collection type data variables with its getter() and setter() methods. It also define a toString() method that represent their values into the console.

Now let’s define the next bean class PersonBean.

PersonBean.java

package com.jwt.spring;

public class PersonBean {
	String name;
	String address;
	int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String toString() {
		return "Person [address=" + address + ", age=" + age + ", name=" + name
				+ "]";
	}
}

The above class defines PersonBean class with its three private data members named age, sex and address with its getter() and setter() methods. This class will be helping to assign customer’s data using collection mechanism in a list, set, map and properties type.

3 . Configuration of bean class.

Consider the bean.xml given below. Here we defined two beans and injecting AccountDetails bean type to BankService bean by using ref element.

bean.xml

<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-3.0.xsd">
	<bean id="CustomerBean" class="com.jwt.spring.CustomerBean">
		<!-- java.util.List -->
		<property name="lists">
			<list>
				<value>1</value>
				<ref bean="PersonBean" />
				<bean class="com.jwt.spring.PersonBean">
					<property name="name" value="sampleList" />
					<property name="address" value="address" />
					<property name="age" value="28" />
				</bean>
			</list>
		</property>
		<!-- java.util.Set -->
		<property name="sets">
			<set>
				<value>1</value>
				<ref bean="PersonBean" />
				<bean class="com.jwt.spring.PersonBean">
					<property name="name" value="sampleSet" />
					<property name="address" value="address" />
					<property name="age" value="28" />
				</bean>
			</set>
		</property>
		<!-- java.util.Map -->
		<property name="maps">
			<map>
				<entry key="Key 1" value="1" />
				<entry key="Key 2" value-ref="PersonBean" />
				<entry key="Key 3">
					<bean class="com.jwt.spring.PersonBean">
						<property name="name" value="sampleMap" />
						<property name="address" value="address" />
						<property name="age" value="28" />
					</bean>
				</entry>
			</map>
		</property>
		<!-- java.util.Properties -->
		<property name="pros">
			<props>
				<prop key="admin">admin@javawebtutor.com</prop>
				<prop key="support">support@javawebtutor.com</prop>
			</props>
		</property>
	</bean>
	<bean id="PersonBean" class="com.jwt.spring.PersonBean">
		<property name="name" value="Mukesh" />
		<property name="address" value="OMR Road" />
		<property name="age" value="29" />
	</bean>
</beans>

The above listing defines bean.xml file to configure collection injection for CustomerBean class using PersonBean class. It defines four PersonBean type beans for four different type of collection tags <list>, <map>, <set> and <props>.

4 . Creating java class to test the program

Test.java

package com.jwt.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"bean.xml");
		CustomerBean cust = (CustomerBean) context.getBean("CustomerBean");
		System.out.println(cust);
	}
}
Output

Customer Detail 
 [by lists=[1, Person [address=OMR Road, age=29, name=Mukesh], Person [address=address, age=28, name=sampleList]], 
 by sets=[1, Person [address=OMR Road, age=29, name=Mukesh], Person [address=address, age=28, name=sampleSet]], 
 by maps={Key 1=1, Key 2=Person [address=OMR Road, age=29, name=Mukesh], Key 3=Person [address=OMR Road, age=29, name=Mukesh]}, 
 by properties={admin=admin@javawebtutor.com, support=support@javawebtutor.com}]


You can download the source code of the example by clicking on the Download link below.

Download


Previous Next Article
comments powered by Disqus