Spring Expression Language – Regular Expressions

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 (6) from the above list. i.e. performing regular expression matching using Spring Expression Language.

The key elements of syntax of Spring Expression Language used for performing regular expression matching 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.
  • Matching with regular expression is done using the matches keyword
  • Regular expression syntax is similar to corresponding syntax in Java language.

The following sample code provides an overview of using Spring Expression Language to perform regular expression matching.

Sample Program Overview

The sample program is based on an email validator that validates whether the email address has a valid format.

We will create the Person class with members as name and email.

We will then create the EmailValidator class with member emailValid.

We will also create the spring-config.xml and use Spring Expression Language to verify whether the email address value is of correct format using regular expression matching.

Finally, we will test our setup using TestSpringExpressionLanguageRegularExpression class which will load Spring context and get a reference to EmailValidator class. We will print the EmailValidator.getEmailValid() on the console to verify that regular expression matching using Spring Expression Language has occured successfully.

Sample Program Overview

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

Create members name and email(see lines 5-6 below) with their accessor methods (see lines 7-18 below)

Create EmailValidator class (see below).

Create member called emailValid (see line 5 below) with its accessor methods (see lines 7-13 below).

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

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

Assign values for name and email properties (see lines 12-13 below).

Declare bean for EmailValidator class (see line 16 below). Populate the value of ’emailValid’ property by using the Spring Expression Language (see lines 18-19 below). In the expression
#{person.email matches ‘[\w]+.[\w]+@[\w]+.com’}
, we use the
keyword to provide the regular expression pattern for email matching. This highlights the usage of regular expression matching using Spring Expression Language.

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

We access the EmailValidator.getEmailValid() method and print the output to the console thereby verifying that regular expression matching has 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 springexpressionlanguageregularexpression-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment

  • (Alternatively you can go the folder containing the springexpressionlanguageregularexpression-installer.jar and execute the jar using
    java -jar springexpressionlanguageregularexpression-installer.jar

  • 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
    . 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