Spring Expression Language – Mathematical Operators

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 (4) from the above list. i.e. performing mathematical operations using Spring Expression Language.

The key elements of syntax of Spring Expression Language used for performing mathematical operations 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.
  • Standard mathematical operations such as +, -, *, /, % etc. are used on numerical properties similar to Java language.

The following sample code provides an overview of using Spring Expression Language to perform mathematical operations.

Sample Program Overview

The sample program uses Spring Expression Language to calculate the perimeter of a Rectangle.

We will create a Rectangle class with members ‘length’ and ‘breadth’.

We will then create a PerimeterCalculator class with member ‘perimeter’.

We will also create the spring-config.xml and use Spring Expression Language to calculate the perimeter of the Rectangle using the formula
Perimeter = 2 * (length + breadth)
.

Finally, we will test our setup using TestSpringExpressionLanguageMathOperators class which will load Spring context and get a reference to PerimeterCalculator class. We will print the perimeter on the console to verify that mathematical operations 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 Rectangle class (see below).

Create members length and breadth (see lines 5-6 below) with their accessor methods (see lines 9-20 below).

Create PerimeterCalculator class (see below).

Create member perimeter (see line 4 below) with its accessor methods (see lines 6-11 below).

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

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

Assign values for length and breadth properties (see lines 11-12 below).

Declare bean for PerimeterCalculator class (see line 16 below). Populate the value perimeter property by using the Spring Expression Language (see line 17 below). In the expression
#{2*(rectangle.length + rectangle.breadth)}
, we refer to mathematical operations
*
and
+
. We also refer to properties of rectangle bean by using the dot (.) notation . This highlights the usage of mathematical operations using Spring Expression Language.



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

We access the perimeterCalculator.getPerimeter() method and print the output to the console thereby verifying that mathematical operations for calculating perimeter have been successfully performed 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 springexpressionlanguagemathoperators-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


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