Spring Expression Language – SpEL API & Example

1. Objective

In our past tutorial, we discussed the Spring AOP Tutorial. Today, in this SpEL Tutorial, we will learn Spring Expression Language. Moreover, we will cover the API in Expression Langauge in Spring Framework and its example.
So, let’s start the Spring Expression Language Tutorial.

Spring Expression Language - SpEL API & Example

Spring Expression Language – SpEL API & Example

Stay updated with latest technology trends
Join DataFlair on Telegram!!

2. Spring Expression Language (SpEL)

SpEL is an expression language which supports the features like querying and manipulating an object graph during the runtime. The Spring 3.0 introduced the Spring Expression Language (SpEL) which had a syntax similar to Unified EL. It can save you a lot of coding since you can dynamically assign values during the runtime. Tha e developers at Spring made SpEL part of what is known as Spring Core. Also, SpEL can be configured through the files and the Bean classes using the XML and Java-based annotation. Therefore, SpEL is available throughout the Spring Framework.
Do you know about Spring Annotation Based Configuration
There are many expression languages available in the market such as OGNL, MVEL etc. The Spring Expression Language is used for providing the Spring community with a well-supported expression language. This can be used across all the products which are available in the Spring Framework portfolio. SpEL also provides some of the additional features from the existing expression languages such as an invocation of methods and string templating functionality.
Some of the functionality supported by the expression language are as follows:

  • Literal expressions
  • Boolean and relational operators
  • Regular expression
  • Accessing properties, arrays, lists and maps
  • Bean references
  • Method invocation
  • User-defined functions
  • Variables

3. Spring Expression Language API

The SpEL API has many interfaces and classes which are defined as follows:

  • Expression interface
  • SpelExpression class
  • ExpressionParser interface
  • SpelExpressionParser class
  • EvaluationContext interface
  • StandardEvaluationContext class
Spring Expression Language - SpEL API & Example

Spring Expression Language – SpEL API & Example

With the above-given APIs, you will see a working example with Eclipse IDE in place.
Hello SpEL example
Have a look at Spring Bean Life Cycle – Initialization and Destruction
In this example, you will create ExpressionParser object. Using that you will create Expression instance and input the message “Hello SPEL”. The message will be displayed using the ExpressionParser object.

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class Test {
public static void main(String[] args) {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello SPEL'");
String message = (String) exp.getValue();
System.out.println(parser.parseExpression("'Hello SPEL'").getValue());

4. SpEL Tutorial – Examples

Now you will see some more useful examples using Spring Expression Language. Following are the examples of SpEL assuming that each of them is written in main() method:

  • Using the concat() along with the String
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Welcome SPEL'.concat('!')");
String message = (String) exp.getValue();
  • Converting the String into a byte array
Expression exp = parser.parseExpression("'Hello World'.bytes");
byte[] bytes = (byte[]) exp.getValue();
for(int i=0;i<bytes.length;i++){
System.out.print(bytes[i]+" ");
  • Converting the contents of String into upper-case letters
Expression exp = parser.parseExpression("new String('hello world').toUpperCase()");
String message = exp.getValue(String.class);
  • Getting the length after converting String into bytes
Expression exp = parser.parseExpression("'Hello World'.bytes.length");
int length = (Integer) exp.getValue();

Do you know about Spring WS Integration
So, this was all about Spring Expression Language Tutorial. Hope you like our explanation

5. Conclusion

Hence, in this SpEL Tutorial, we studied the API in Spring Expression Language. At last, we covered the example of Spring Expression Language. Furthermore, if you have any query, feel free to ask in the comment section.
Related Topic- Integration of MVC with Spring
For reference

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.