Spring Expression Language – Referencing Static Methods and Members

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 (3) from the above list. i.e. Invoke static methods using Spring Expression Language.

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

  • All Spring Expresions should be declared inside ${…}
  • Static class is referred by using T(…)
  • 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 invoke a static method.

Sample Program Overview

The sample program is modelled on a random number generator.

We will create a RandomNumberGenerator.class used to generate random numbers between 0 and 1.


We will create the spring-config.xml and use Spring Expression Language to inject a random number into RandomNumberGenerator class using
java.lang.Math.random()
static method.

Finally, we will test our setup using TestSpringExpressionLanguageStatic class which will load Spring context and get a reference to RandomNumberGenerator class. We will print the random number on the console to verify that dependency injection using static methods has occured successfully.

Note: Although the sample program refers to accessing static methods, the same rationale is also applicable for accessing static constants.

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

Create a member called randomNumber (see line 4 below) with its accessor methods (see line 6-12 below).

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

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

Populate the value randomNumber property by using the Spring Expression Language (see line 11 below). In the expression
#{T(java.lang.Math).random()}
, we use refer to the
java.lang.Math
class by using the
#{T{java.lang.Math}}
and refer to its static method
random()
by using the dot (.) notation . This highlights the usage of referring a static method using Spring Expression Language.



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

We access the RandomNumberGenerator.getRandomNumber() method and print the output to the console thereby verifying that the static method (i.e.
java.lang.Math.random()
has been successfully injected using Spring Expression Language (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 springexpressionlanguagestaticmethod-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


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