Advertisement
Interfaces and classes in SpEl | Description |
---|---|
Expression |
An interface which encapsulates an SpEL expression. |
SpelExpression |
A class which implements the Expression interface and it refers to an SpEL expression. |
SpelExpressionParser |
A class which is used to parse the SpEL expression. |
StandardEvaluationContext |
This class represents the context used for evaluation of an expression. An expression which is not a String but an object of a class. i.e. it evaluates an expression in the context of an object of a class. |
package decodejava;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class Utility
{
int id;
public static void main(String[] args)
{
SpelExpressionParser parser = new SpelExpressionParser();
SpelExpression expression = parser.parseRaw("12*4");
System.out.println("Value of first expression : " + expression.getValue());
expression = parser.parseRaw("100<40");
System.out.println("Value of second expression : " + expression.getValue());
expression = parser.parseRaw("'Hello' matches 'he'");
System.out.println("Value of third expression : " + expression.getValue());
expression = parser.parseRaw("'BlueSky'");
System.out.println("Value of fourth expression : " + expression.getValue());
expression = parser.parseRaw("1000");
System.out.println("Value of fifth expression : " + expression.getValue());
expression = parser.parseRaw("19.999");
System.out.println("Value of sixth expression : " + expression.getValue());
expression = parser.parseRaw("99.9f");
System.out.println("Value of sixth expression : " + expression.getValue());
expression = parser.parseRaw("'Welcome' matches 'Wel.*'");
System.out.println("Value of seventh expression : " + expression.getValue());
}
}
Advertisement
Value of first expression : 48
Value of second expression : false
Value of third expression : false
Value of fourth expression : BlueSky
Value of fifth expression : 1000
Value of sixth expression : 19.999
Value of sixth expression : 99.9
Value of seventh expression : true
package decodejava;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class Employee
{
private int id;
public void setId(int id)
{
this.id = id;
}
public String getMessage()
{
return("Welcome ID " + id);
}
public static void main(String[] args)
{
Employee ob = new Employee();
//Passing the object of Employee class to StandardEvaluationContext, which is going to evaluate the expressions in the context of this object.
StandardEvaluationContext stContext = new StandardEvaluationContext(ob);
//Creating an object of SpelExpressionParser class, used to parse the SpEL expression
SpelExpressionParser parser = new SpelExpressionParser();
//Calling parseRaw() method of SpelExpressionParser, which parses the expression and returns an SpelEpression object
SpelExpression expression = parser.parseRaw("id");
//Calling setValue() method sets the value of "id" with the value 1 in the context of object of Employee.
expression.setValue(stContext,"1");
System.out.println("Message for Employee : " + ob.getMessage());
}
}
Message for Employee : Welcome ID 1
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement