Tuesday, November 4, 2014

Spring Tutorial 12 – Use Collections In Spring

Spring also provides to use collections framework, values can be injected into collections type (List, Set, Map, and Properties). Spring supports majorly four types of collections.
All collections which spring uses have same definition as defined in java.

  • List <list>: A list of values can be injected while having the duplicates.
  • Set <set>: A list of values can be injected while having the no duplicate elements.
  • Map <map>: In this collection a key-value pair elements are injected, which can be of any types.
  • Property <props>: A name-values pair collection are injected in this property tag, this tag has condition that both name and value must be of String type.
In this tutorial we will see that how list collection holds the reference of beans. List collection can be used to hold the values as well.
Let’s get into this detail with following block of code.
Let’s create a project say “SpringTutorial” now add the required jar support to spring, to add the jar you can follow the Spring Tutorial 02 -My First 'Hello World' Program in Spring blog.
Now create a package with the name of “org.javaIsEasy.springCollectionExample“.
Now create a java file with the name of “Rectangle.java” in this package.

package org.javaIsEasy.springCollectionExample;
public class Rectangle {
            private int length;
            private int width;
            public int getLength() {
                        return length;
            public void setLength(int length) {
                        this.length = length;
            public int getWidth() {
                        return width;
            public void setWidth(int width) {
                        this.width = width;
Create another java file in the same package with the name of “Area.java”.
In the Area file we will be creating object of the Rectangle class, we will give the reference of the Rectangle class in this Area class. So let’s see how it goes.

package org.javaIsEasy.springCollectionExample;
import java.util.List;
public class Area {
            private List<Rectangle> rectangle;
            public List<Rectangle> getRectangle() {
                        return rectangle;
            public void setRectangle(List<Rectangle> rectangle) {
                        this.rectangle = rectangle;

Now create another java file with the name of “CallAreaApplication.java” in the same package.
This class has the main method; configuration file will be loaded in this class and let’s see how bean is called here.

package org.javaIsEasy.springCollectionExample;
import java.util.ArrayList;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CallAreaApplication {
public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("org/javaIsEasy/springCollectionExample/Beans.xml");
            Area area=(Area)context.getBean("AreaBean");
            ArrayList<Rectangle> rectangleDataList=(ArrayList<Rectangle>) area.getRectangle();
                        System.out.println("Small Area ------> Length : "+rectangleDataList.get(0).getLength()+" And Width : "+rectangleDataList.get(0).getWidth());
                        System.out.println("Medium Area ------> Length : "+rectangleDataList.get(1).getLength()+" And Width : "+rectangleDataList.get(1).getWidth());
                        System.out.println("Big Area ------> Length : "+rectangleDataList.get(2).getLength()+" And Width : "+rectangleDataList.get(2).getWidth());

Now we have to create bean configuration file with the name of “Beans.xml” in the same package.

Here the Beans.xml file is used to define spring bean configuration. The following code shows how to create inner beans.


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

   <bean id="AreaBean" class="org.javaIsEasy.springCollectionExample.Area">
      <property name="rectangle">
            <ref bean="smallArea"/>
            <ref bean="mediumArea"/>
            <ref bean="bigArea"/>

            <bean id="smallArea" class="org.javaIsEasy.springCollectionExample.Rectangle">
      <property name="length" value="3"/>
      <property name="width" value="2"/>
   <bean id="mediumArea" class="org.javaIsEasy.springCollectionExample.Rectangle">
      <property name="length" value="6"/>
      <property name="width" value="4"/>
   <bean id="bigArea" class="org.javaIsEasy.springCollectionExample.Rectangle">
      <property name="length" value="8"/>
      <property name="width" value="6"/>

Structure of the project :


Let’s run this program to get the result:


smallArea length–>3, width–>2
mediumArea length–>6, width–>4
bigArea length–>8, width–>6