Spring Expression Language – Referencing Beans

Concept Overview

In general, most of the beans declared for dependency injection using Spring are static and 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
  6. Perform Regular Expression Matching

In this tutorial, we will focus on points (1) and (2) from the above list. i.e. Refer to bean by its
id
attribute and access properties and methods defined in a bean.

The key elements of syntax of Spring Expression Language are:

  • All Spring Expresions should be declared inside ${…}
  • Any bean can be directly accessed using the id attribute of the bean.
  • 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.

The following sample code provides an overview of using Spring Expression Language to refer to a bean, access its members and methods directly from within the spring configuration file.

Sample Program Overview

The sample program is modelled on an MP3 player which contains a Play List of songs.

We will create PlayList class which contains a List of songs as a member.

We will also define a method called getFirstSong() in PlayList class which returns the first song in the play list.

We will create an Mp3Player class with two members: songsInPlayList and firstSong.

We will create the spring-config.xml and use Spring Expression Language to inject the value of PlayList.songs property into Mp3Player.songsInPlayList property and PlayList.getFirstSong() into Mp3Player.firstSong property.

In this way, we will use Spring Expression Language to refer to the PlayList bean by id, inject PlayList.songs property and invoke PlayList.getFirstSong() method into the properties of Mp3Player.

Finally, we will test our setup using TestSpringExpressionLanguageReferenceBeans class which will load Spring context and get a reference to Mp3Player class. We will print the songs in play list of the Mp3Player and the first song in the Mp3Player to verify that dependency injection of bean reference, bean property and bean method invocation using Spring Expression Language has happened 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 PlayList class with member songs (see line 7 below) with accessor methods (see lines 9-15 below).

Create the PlayList.getFirstSong() method (see line 17 below) which returns the first song in the play list (see line 18 below).

Create the Mp3Player class with members called songsInPlayList and firstSong (see lines 6-7 below).

Create accessor methods for songsInPlayList and firstSong (see lines 9-20 below).

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

Declare bean for PlayList class (see line 10 below).

Populate the songs property by using
list
tag and add names of some songs (see lines 11-17 below).

Declare the bean for Mp3Player (see line 20 below).

Populate the value songsInPlayList property by using the Spring Expression Language (see line 21 below). In the expression
#{playList.songs}
, we use refer to the PlayList class by using the its id
playList
and refer to its member
songs
by using the dot (.) notation . This demonstrates the usage of referring a bean by its id and accessing its property.

Populate the value firstSong property by using the Spring Expression Language (see line 22 below). In the expression
#{playList.getFirstSong()}
, we use refer to the PlayList class by using the its id
playList
and refer to its method
getFirstSong()
by using the dot (.) notation. This demonstrates the usage of referring a bean by its id and accessing its method.



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

We access the Mp3Player.getSongsInPlayList() method and print the output to the console thereby verifying that the songs in play list have been successfully injected using Spring Expression Language (see line 13 below).

We access the Mp3Player.getFirstSong() method and print the output to the console thereby verifying that the first songs in the play list has been successfully injected using Spring Expression Language (see line 14 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 springexpressionlanguagereferencingbeans-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


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

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.