Learn How to Work With Spring Expression Language (EL)

0
3744
Spring Expression

Spring Expression

The Spring Expression Language, abbreviated as SpEL, is basically an expression language that is also very powerful in its workings and offers support for queries and manipulation of an object graph during runtime. The feature was released with Spring 3.0 in 2009.

While the syntax of the language is pretty similar to Unified EL, it does offer more features. Of them, the most popular ones are string templates and method invocation. There are several Java expression languages available, including JBoss EL, MVEL, ONGL and more. However, the creation of a separate Spring Expression Language was to ensure that the Spring community had a single supportive expression language that could be used across everything in the Spring framework. Many of its features are largely due to the requirements that projects are likely to have in the Spring environment, such as tooling for code completion in the Spring Tool Suite. SpEL is based on agnostic API, meaning other expression languages can be integrated when it is necessary to do so. Despite Spring being the foundation framework on which expression evaluation works, it is not dependent on it. It is independent and can be viewed as an independent expression language.

Why SpEL?
Considering that there are several languages that can be used for the evaluation of expressions, Spring does stand out for a few reasons. Here are a few reasons as to why you should consider Spring Expression Language.

  • Syntax: – SpEL can be used in both annotation metadata (Spring configuration) and XML. In both the cases, the designation takes place using the syntax #{“expression string in SpEL”} for runtime evaluation.
  • Reduces code: – SpEL is a very simple alternative to many of the other expression languages that are there. In fact, it can reduce a lot of code for you.
  • Simplified: – When using SpEL, the coder only needs to take care of writing the expressions in certain properties file. The framework code is responsible for extraction handling of the new data.

What are the features?
Spring Expression Language comes with a number of features which have been mentioned below –

  • Regular expressions
  • Literal expressions
  • Class expressions
  • Boolean and relational operators
  • Assignment operators
  • Templated expressions
  • Method invocation
  • Calling constructors
  • Collection selection and projection
  • Variables
  • Ternary operator
  • Array construction
  • User defined functions
  • Inline lists
  • Bean references
  • Accessing arrays, lists, properties, maps

Some of the features will be covered in the following examples. The other features can also be tested using sample applications. But the application structure will remain the same.

Environment setup: –
Setting up the environment for Spring based application development involves just three major steps.

  • Setting up Java Development Kit: –
    Download JDK from the Oracle site and then, install and configure it. Set the PATH and JAVA_HOME environment variables.
  • Setting of Eclipse IDE: –
    Eclipse can be downloaded from the official website. Once downloaded, unpack the binaries and then set the PATH as well.
  • Setting up Spring libraries: –
    The Spring libraries may be obtained from http://repo.spring.io/release/org/springframework/spring/. Again, it is important to set the CLASSPATH correctly.
    Now your environment is ready to start developing Spring based Java applications.

Sample application: –
In this section, we will explore the expression language used in an XML file. The expression language can also be used as annotation in Java file. If the EL is used as annotation, then XML file is not required. EL is generally written as #{SpEL expression}
Following is a simple company bean having reference of Product bean. It contains product name and price as shown below.

Listing 1: – Company bean

Following is the product bean having name and price. It is referred in company bean above.

Listing 2: – Product bean

Now, this is the XML file. Here product bean, product name and product price is mentioned as expression language. The default values are also set in the XML file. So, these will be available in the company bean also. Please check how the expression language syntax is used in the bean file. Similar syntax is also used in case of annotation based program.

Listing 3: – Configuration XML

This is the main Java application. It gets reference of the company bean and then prints the product name and price. Some more properties and their values can also be injected in a similar way and used in the sample application.

Listing 4: – Main Java program to test

Once you run the application, the following output will be display on the console.
Showing output
Conclusion: –
Spring Expression Language can be a great option if you are looking for an expression languages that is similar to the Unified EL. SpEL allows you to dynamically assign values at runtime and therefore saves a lot coding on the part of the programmer. Besides, it can also work on two different configuration schemes – XML and annotation based. So, any option can be selected as per your need. Spring EL is very powerful and it can be extended to add more functionality in your application.

LEAVE A REPLY

Please enter your comment!
Please enter your name here