Spring Expression Language – Logical Operators

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 (5) from the above list. i.e. performing relational/ logical operations and conditional evaluations using Spring Expression Language.

The key elements of syntax of Spring Expression Language used for performing relational and logical 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 relational operations such as <, <=, ==, >=, > etc. similar to Java language should be used.
  • Logical operations such as and, or, not should be used.

The following sample code provides an overview of using Spring Expression Language to perform relational and logical operations including conditional evaluation.

Sample Program Overview

The sample program provides the result of an examination for a student based on his marks in mathematics, physics and chemistry. The result is ‘passed’ if the student has more than 40 marks in every subject; else the result is ‘failed’. If the result is ‘passed’ then the result message is ‘Congratulations: You have passed!’ or else the message is ‘Sorry: You have failed.’.

We will create a MarkSheet class with members: studentName, marksInMathematics, marksInPhysics and marksInChemistry.

We will create the ExaminationResult class with members as hasPassed and resultMessage.

We will also create the spring-config.xml and use Spring Expression Language to calculate value of ExaminationResult.hasPassed property using relational and logical operations. We will also calculate the value of ExaminationResult.resultMessage property using conditional evaluation.

Finally, we will test our setup using TestSpringExpressionLanguageLogicalOperators class which will load Spring context and get a reference to ExaminationResult class. We will print the ExaminationResult.getHasPassed() and ExaminationResult.getResultMessage() on the console to verify that relational/logical operations using conditional evaluation via 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 MarkSheet class (see below).

Create members studentName, marksInMathematics, marksInPhysics and marksInChemistry (see lines 5-9 below) with their accessor methods (see lines 12-35 below).

Create ExaminationResult class (see below).

Create members hasPassed and resultMessage (see lines 5-6 below) with its accessor methods (see lines 7-18 below).

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

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

Assign values for studentName, marksInMathematics, marksInPhysics and marksInChemistry properties (see lines 12-15 below).

We declare two String beans for result messages: one for ‘passed’ and one for ‘failed’ (see lines 19-25 below).

Declare bean for ExaminationResult class (see line 27 below). Populate the value of ‘hasPassed’ property by using the Spring Expression Language (see lines 29-32 below). In the expression
#{markSheet.marksInMathematics >= 40 and markSheet.marksInPhysics >= 40 and markSheet.marksInChemistry >= 40}
, we refer to relational operation
>=
. We also refer to logical operations like
and
. This highlights the usage of relational and logical operations using Spring Expression Language.

Populate the value of ‘resultMessage’ property by using the Spring Expression Language (see lines 33-36 below). In the expression
#|markSheet.marksInMathematics >= 40 and markSheet.marksInPhysics >= 40 and markSheet.marksInChemistry >= 40 ?passedMessage:failedMessage}
, we use the conditional evaluation by using the
? :
. operator (similar to Java language syntax). This highlights the usage of conditional evaluation using Spring Expression Language.



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

We access the ExaminationResult.getHasPassed() and examinationResult.getResultMessage() methods and print the output to the console thereby verifying that relational/logical operations and conditional evaluation have been successfully performed using Spring Expression Language (see lines 13-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 springexpressionlanguagelogicaloperators-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


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