Spring Expression Language – Accessing Collection – Map

Pre-requisite trails

Spring Setter Injection
Spring Injection of Map

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.Map
type in this tutorial and access values in the map using their keys.

The key elements of syntax of Spring Expression Language used for accessing Map 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 Map are accessed by referring to the corresponding key using [] notation.

The following sample code provides an overview of using Spring Expression Language to access elements in a Map.

Sample Program Overview

The sample program is based on Telephone Directory where the phone number of a person named ‘Alba’ is accessed by referring to his name.

We will create the TelephoneDirectoryAccessor with member as albaPhoneNumber.

We will also create the spring-config.xml to create a Map called telephoneDirectory.

We will then access the telephoneDirectory map and access its element using Spring Expression Language.

Finally, we will test our setup using TestSpringExpressionLanguageAccessMap class which will load Spring context and get a reference to TelephoneDirectoryAccessor class. We will print the telephone number of Alba on the console to verify that accessing elements of a Map 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 the TelephoneDirectoryAccessor (see sample code below).

Create member albaPhoneNumber (see line 5 below)

Create accessor methods for albaPhoneNumber (see lines 7-13 below).

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

Declare bean for telephoneDirectory of type
java.util.HashMap
and populate the map with names and telephone numbers (see lines 10-18 below)

Declare bean for telephoneDirectoryAccessor (see line 20 below)

Access the phone number for Alba (see line 21 below). In the expression
#{telephoneDirectory[‘Alba’]}
, we use the name of the map
telephoneDirectory
and refer to the key named ‘Alba’ to get the corresponding phone number. This highlights the usage of accessing individual elements of Map using Spring Expression Language.


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

We access the telephoneDirectoryAccessor.getAlbaPhoneNumber() method and print the output to verify that accessing of elements of Map using Spring Expression Language has occurred 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 springexpressionlanguageaccessmap-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


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