Site icon DataFlair

Spring Expression Language – SpEL API & Example

FREE Online Courses: Dive into Knowledge for Free. Learn More!

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

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:

3. Spring Expression Language API

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

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:

ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Welcome SPEL'.concat('!')");
String message = (String) exp.getValue();
System.out.println(message)
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]+" ");
}
Expression exp = parser.parseExpression("new String('hello world').toUpperCase()");
String message = exp.getValue(String.class);
System.out.println(message);
Expression exp = parser.parseExpression("'Hello World'.bytes.length");
int length = (Integer) exp.getValue();
System.out.println(length);

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

Exit mobile version