Spring Injection – List

Concept Overview

Sometimes, it is necessary to inject a Collection of type
java.util.List
into a bean using Spring injection. e.g. A ToDoList may contains a list of tasks.

This is achieved by using the
list
tag within the
property
tag in Spring configuration.

The following sample program provides an overview of injecting a List of tasks into a ToDoList bean.

Sample Program Overview

We will create a ToDoList class.

We will also create a member of ToDoList class called ‘tasks’ of type
java.util.List
.

Using Spring’s configuration, we will inject some values into the tasks list (e.g. ‘Purchase grocery’, ‘Pay bills’ and ‘Visit friend’)

Finally, we will test our setup using TestInjectionList class which will load Spring context and get a reference to ToDoList class. We will fetch the tasks from ToDoList and print them in the console to verify that the list of tasks has been successfully injected.

Note:

  • Although we have used setter injection in this example, it is also possible to inject java.util.List using constructor injection.
  • Although we used simple types within the list using value tag, we can also use bean references within list using ref tag.

Required Libraries
  • commons-logging.jar
  • log4j.jar
  • org.springframework.aop.jar
  • org.springframework.asm.jar
  • org.springframework.beans.jar
  • org.springframework.context.jar
  • org.springframework.context.support.jar
  • org.springframework.core.jar
  • org.springframework.expression.jar

Source Code

Create the ToDoList class (see below).

Create a member called ‘tasks’ of type
java.util.List
(see line 7 below)

Create accessor methods for ‘tasks’ (see lines 9-15 below)

Create the spring-config.xml file (see below).

Declare bean for ToDoList class (see line 10 below)

For the ‘tasks’ property (see line 11 below), we define a
list
tag (see line 12 below).

We populate the list using the
value
tag (see lines 13-15 below).


Finally, we need a java program to test the our setup.This is done by TestInjectionList.java (see source code below).

We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans (see line 11 below).

We get the reference to ToDoList class through Spring using the bean name ‘toDoList’ (see line 12 below).

We access the ToDoList.getTasks() method and output the list contents to verify that the list has been injected successfully (see line 13 below).


Running Sample Program

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.

Download And Automatically Run Sample Program
  • Save the springinjectionlist-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springinjectionlist-installer.jar and execute the jar using
    java -jar springinjectionlist-installer.jar
    command)

  • You will see a wizard as shown below
  • Enter the location of the directory where you want the program to install and run (say, C:\Temp)
  • The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
  • Browsing the Program

    This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
    springinjectionlist
    . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.

    Leave a Comment