Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to inject objects using collections in Spring

In this article, we will learn why we require collections in spring and how do we inject collections in Spring.

Collections are very useful and frequently used with java technology. Generics also made it very easy to use collection with auto-boxing and un-boxing functionality. Collections are used to manipulate objects easily with java. In spring, collections are used to manage objects instead of using primitive data types.

Injecting Collections with spring in detail

Before collection, we used to pass single value at a time. If we are required to pass multiple or bundle of data to be passed like list, set, and map etc. To manage such kind or problem, spring provides a way of injecting collections. We can inject list, map, set and pros type collections in spring. There are separate tags are used for these type of collection types as listed:

  • <list> : This tag is used to inject list of object.
  • <map> : This tag is used to inject bundled object as map.
  • <set> : This tag is used to inject object stored in Set.
  • <props> : This tag is used to inject collection of object in value - value pair fashion.

Required Tools & Application:

In this article, we will be using:

  • Eclipse IDE
  • Spring Jar Files Lib
  • JDK Environment
  • Maven Configuration

We can pass values to spring using collection in two different ways:

  • Passing direct values from collections
  • Passing reference of bean as a collection

Now let’s go with listed steps:

  • Create a project in eclipse with name “CollectionInject” with package name com.test.collect under the src folder.
  • Now Lets add a set of required spring jar files in the lib directory or use maven to pull them out from the repository.
Required Spring Jar files

Figure 1: Required Spring Jar files

The above figure list the jar files, which are required to run this application. Maven is used to pull these jar files from the maven repository. To pull these jar files, we are required to define a xml file named pom.xml file that defines the archetype to work on spring 2.5 version and its dependent jar files that will help to run this application. Liting 1 shows the pom.xml definition as listed below:

Listing 1: 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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.collection.core</groupId>
	<artifactId>CollectionInjection</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name> CollectionInjection</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- Spring framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6</version>
		</dependency>

	</dependencies>
</project>

Listing 1 defines pom.xml that defines set of xml tag that helps to pull the required jar files from the maven repository. We define a tag <dependency> that pulls spring arctype 2.5 version jar files from the maven repository and creates required directory structure for spring desktop application. Tag <groupId> defines the package name and the <artifactId> defines the project name that will be created. To make this project to work with Eclipse IDE compatible, open command prompt and place command into this project directory as: mvn eclipse:eclipse

And hit enter. This will download the required jars and will create an eclipse environment to work with this project.

Now create a bean class into the package “com.collection.core” and create two bean files as listed in Listing 1 and Listing 2 respectively:

Listing 2: CustomerBean.java

Package com.collection.core;

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 Detail \n [by lists=" + lists + ", \n by sets=" + sets + ", \n by maps=" + maps
				+ ", \n by 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 into the Listing 3:

Listing 3: PersonBean

Package com.collection.core;

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;
	}

}

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.

To represent this data and configure injecting collections with the spring, create a main() that will invoke springbean.xml file. Listing 4 defines the Main.java class file as:

Listing 4: Main.java

Package com.collection.core;

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

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"SpringBeans.xml");

		CustomerBean cust = (CustomerBean) context.getBean("CustomerBean");
		System.out.println(cust);
	}
}

Above listing defines Main.java class with main() method that invoke springbean.xml file and create an instance of CustomerBean class using that instance of xml file invoked and invoke toString() method of CustomerBean class.

Now lets define SpringBeans.xml file in Listing 5 as below:

Listing 5: SpringBean.java

<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-2.5.xsd">

	<bean id="CustomerBean" class="com.collection.core.CustomerBean">

		<!-- java.util.List -->
		<property name="lists">
			<list>
				<value>1</value>
				<ref bean="PersonBean" />
				<bean class="com.collection.core.PersonBean">
					<property name="name" value="UmeshList" />
					<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.collection.core.PersonBean">
					<property name="name" value="UmeshSet" />
					<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.collection.core.PersonBean">
						<property name="name" value="UmeshMap" />
						<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@nospam.com</prop>
				<prop key="support">support@nospam.com</prop>
			</props>
		</property>

	</bean>

	<bean id="PersonBean" class="com.collection.core.PersonBean">
		<property name="name" value="Umesh" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>

</beans>

The above listing defines SpringBean.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>.

Output:

To execute this application, we are required to run Main.java class file as java application that will represent output as shown in Figure 2:

Main.java executes collection injection of CustomerBean class

Figure 2: Main.java executes collection injection of CustomerBean class

Conclusion

In this article, we learn the requirement of using collection with spring framework. We also learn the way of injecting collection in spring.



Working in Software Development domain from 7 years now and is well equipped with programming languages like HTML, CSS, Java, PHP, .NET etc.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login