Spring Expression Language – Accessing Collection – List

Pre-requisite trails

Spring Setter Injection

Spring Constructor Injection

Spring Injection of List

Concept Overview

In general, most of the beans declared for dependency injection using Spring are statically defined i.e. It is known a-priori what the dependencies are going to be.

However, in certain advanced situations, there may be a requirement to perform dependency injection dynamically at runtime. Such dynamic dependency injection is possible in Spring using
Spring Expression Language
.

Using Spring Expression Language, we can:

  1. Refer to other beans by id attribute
  2. Refer to the properties and invoke methods defined in other beans
  3. Refer to the static constants and invoke static methods
  4. Perform Mathematical operations on values
  5. Perform Relational and Logical comparisons including Conditional Evaluation
  6. Perform Regular Expression Matching
  7. Accessing Collections

In this tutorial, we will focus on points (7) from the above list. i.e. accessing collections using Spring Expression Language. In particular, we shall use the
java.util.List
type in this tutorial.

Also, we shall cover the following 3 features of accessing a List using Spring Expression Language:

  • Access individual elements of List
  • Perform filtering operations on members of elements in List
  • Access members of elements in List and project them onto another List

The key elements of syntax of Spring Expression Language used for accessing List are:

  • All Spring Expresions should be declared inside ${…}
  • Members and methods of a bean are accessed using the dot (.) notation. This is similar to the way members and methods are accessed in Java language.
  • Individual elements within a List are accessed by using [] notation.
  • Filter operations on elements in a List are performed using .?[] notation.
  • Projection of elements in a List is performed using .![] notation.

The following sample code provides an overview of using Spring Expression Language to access elements in a List, perform filtering and projections operations on a List.

Sample Program Overview

The sample program is based on a Student List.

We will create the Student class with members as name and marks.

We will then create the StudentListAccessor class with members thirdStudentInList, failedStudents and studentNames.

We will also create the spring-config.xml and use Spring Expression Language to create a List of Students.

We will then populate the members of StudentListAccessor to showcase how elements of a List accessed, how filtering of List is achieved and how projection of List is performed using Spring Expression Language.

Finally, we will test our setup using TestSpringExpressionLanguageAccessList class which will load Spring context and get a reference to StudentListAccessor class. We will print the third element of the List, the list of failed students and the list of student names on the console to verify that accessing collections using Spring Expression Language has occured successfully.

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 Student class (see below).

Create members name and marks (see lines 5-6 below) with their accessor methods (see lines 8-19 below)

Override the toString() method to return the String representation of Student object (see lines 21-24 below)

Create StudentListAccessor class (see below).

Create members called thirdStudentInList (which returns the 3rd student in the list), failedStudents (a list of all students with marks less than 40) and studentNames (a projection of list of names of all students) (see lines 8-10 below) with their respective accessor methods (see lines 12-29 below).

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

Declare 5 beans of Student class with different names and marks for each student (see lines 11-39 below).

Create a List of Students for 5 beans defined above (see lines 41-51 below)

Declare bean for StudentListAccessor class (see line 53 below).

Populate the value of ‘thirdStudentInList’ property by using the Spring Expression Language (see line 55 below). In the expression,
#{studentList[2]}
, we access the third element of the list using
[]
notation. This highlights the usage of accessing individual elements of List using Spring Expression Language.

Populate the value of ‘failedStudents’ property by using the Spring Expression Language (see line 56 below). In the expression,
#{studentList.?[marks lt 40]}
, we filter out students in the List with marks less than 40 the using
.?[]
operator. This highlights the usage of performing filter operations on List using Spring Expression Language.

Populate the value of ‘studentNames’ property by using the Spring Expression Language (see line 57 below). In the expression,
#{studentList.![name]}
, we project the student list onto another list containing only the names of all students using the
.![]
operator. This highlights the usage of performing projections onto a List using Spring Expression Language.


Finally, we need a java program to test the our setup.This is done by TestSpringExpressionLanguageAccessList.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 StudentListAccessor class through Spring using the bean name ‘studentListAccessor’ (see line 12 below).

We access the StudentListAccessor.getThirdStudentInList(), StudentListAccessor.getStudentNames() and StudentListAccessor.getFailedStudents() methods and print the outputs to the console thereby respectively verifying accessing of collections, performing filtering operations and performing projections onto List have been successfully happened using Spring Expression Language (see lines 13-15 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 springexpressionlanguageaccesslist-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


  • (Alternatively you can go the folder containing the springexpressionlanguageaccesslist-installer.jar and execute the jar using
    java -jar springexpressionlanguageaccesslist-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
    springexpressionlanguageaccesslist
    . 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