Expression Language of Spring (SpEL)

Posted on by By Somen Sarkar, in Business Intelligence | 0

Spring Expression Lanaguage (SpEL)

 

There are many expression languages available such as JSP EL, OGNL, MVEL and JBoss EL. SpEL provides some additional features such as method invocation and string templating functionality.

 

The Spring Expression Language (SpEL) supports querying and manipulating an object graph at runtime. This expression language is not directly tied to Spring and can be used independently.

 

The following functionality is supported

Literal expressions , Boolean and relational operators , Regular expressions , Class expressions , Accessing properties, arrays, lists, maps , Method invocation ,Relational operators , Assignment , Calling constructors , Ternary operator , Variables , User defined functions , Collection projection , Collection selection ,
 Templated expressions.

 

In Spring Expression Language, we can reference a bean and its nested properties using a ‘dot (.)’

for example #{ T(java.lang.Math).random() * 100.0 } 

Using toUpperCase() method with String

ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Welcome'.toUpperCase()");
String message = (String) exp.getValue();
System.out.println(message);
o/p WELCOME

 

Evaluating a boolean operation

Expression exp = parser.parseExpression("5==3 and 'text'=='test' ");
boolean result = exp.getValue( Boolean.class);
System.out.println(result);
o/p false

 

We can use many operators in SpEL such as arithmetic, relational, logical etc.
Spring EL supports most of the standard mathematical, logical or relational operators.

Relational operators – equal (==, eq), 
not equal (!=, ne), 
less than (<, lt),
 less than or equal (<= , le), 
greater than (>, gt), 
and greater than or equal (>=, ge).
Logical operators:– and, or, and not (!).
Mathematical operators:- 
addition (+),
Subtraction (-),
Multiplication (*), 
division (/),
modulus (%) and
 exponential power (^).

 

In SpEL, we can store a value in the variable and use the variable in the method and call the method. To work on variable, we need to use StandardEvaluationContext class.

 

Example of Using variable in SPEL

public class Rectangle {
private int length;
private int width;
public int getLength() {
return length;
}
public int getWidth() {
return width;
}
public void setLength(int length) { this.length = length; } 
public void setWidth(int width) { this.width = width; } public int area(){ return length*width; } }

The above class is a bean which is used in the Expression Language in the below class

import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Test {
public static void main(String[] args) {
Rectangle rectangle= new Rectangle();
StandardEvaluationContext context=new StandardEvaluationContext(rectangle);

ExpressionParser parser = new SpelExpressionParser();
parser.parseExpression("length").setValue(context,"5");
parser.parseExpression("width").setValue(context,"15");

System.out.println(rectangle.area());
}
}

Spring expression language efficiently sets the length and width property of the bean to 5 and 15 then the area method is invoked in the bean it gives the appropriate result.

Sources: internet

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments