0% found this document useful (0 votes)
15 views4 pages

SP EL

Uploaded by

imrit33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

SP EL

Uploaded by

imrit33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

What is Expression Language?

➢ Expression Language is a programming language.


➢ It provides simplified syntaxes to manipulate Objects and their properties.

Example:

1. JSP EL: JSP(Java Server Page) uses JSP EL to evaluate the objects and properties like request,
session, application etc. and their parameters and attributes.

2. Struts2.x -> OGNL: From struts 2.x version onwards struts framework uses OGNL to evaluate
the objects and their properties like Value Stack, CentralContext, request, application etc.

3. JBoss EL: To evaluate the objects and their properties which are related to JBoss
implementations.

4. SpEL: To evaluate Spring Bean Objects and their properties in Spring Application.

Spring Expression Language (SpEL):

➢ It is an Expression Language.
➢ It has provided simplified syntax to manipulate object and their properties during the
runtime of the application.
➢ In Spring Framework, SpEL is introduced in Spring 3.x with a separate module i.e., Spring
Expression.
➢ You need spring-expression-<version>.jar file in your classpath to work with SpEL.

Spring Expression JAR file dependency for MAVEN:


https://mvnrepository.com/artifact/org.springframework/spring-expression

Main Intentions in SpEL:

a) Write the expression


b) Evaluate the expression

To prepare and Evaluate Expressions in SpEL, Spring has provided very good Predefined Library in
the form of "org.springframework.exprssion" package.

Steps to prepare and evaluate expressions:

Step 1. Create ExpressionParser object

To Convert SpEL into Spring/Java understandable format we need ExpressionParser.


ExpressionParser can manage expressions and have expression evaluation mechanisms.
To represent Expression Parser Spring Framework has provided a predefined interface in the form of
org.springframework.expression.ExpressionParser .
For ExpressionParser interface, Spring framework has provided a predefined implementation class in
the form of "org.springframework.expression.spel.standard.SpelExpressionParser" .

Syntax: ExpressionParser parser = new SpelExpressionParser();

Note: ExpressionParser can evaluate the expressions by using StandardEvaluationContext. It can


evaluate the expressions against objects by preparing Object Graphs internally.

Step 2. Create Expression object

In SpEL, Expression object can represent single Expression. To represent Expression, Spring has
provided a predefined interface in the form of "org.springframework.expression.Expression". For
Expression interface Spring has provided a predefined implementation class in the form of
"org.springframework.expression.spel.standard.SpelExpression".

To prepare expression and to get Expression object we have to use the following method from
ExpressionParser.

public Expression parseExpression(String expression)

Syntax: Expression expression = parser.parseExpression("10+10");

Step 3. Calculate & Get the result from the expression.

To calculate & get expression result we have to use the following method from Expression.

public Object getValue()

Syntax: int val = (int) expression.getValue();


Basic Example:

Test.java

public class Test {


public static void main(String[] args) {
ExpressionParser parser = new SpelExpressionParser();

// Prepare the expression


Expression expression = parser.parseExpression("10+10");

// Evaluate and Get the result


System.out.println(expression.getValue());
}
}

Example with EvaluationContext:

CalculatorBean.java

@Setter
@Getter
public class CalculatorBean {

private int num1;


private int num2;

public int add() {


return num1 + num2;
}

public int sub() {


return num1 - num2;
}

public int mul() {


return num1 * num2;
}

}
Test.java

public class Test {


public static void main(String[] args) {
CalculatorBean bean = new CalculatorBean();

EvaluationContext context = new StandardEvaluationContext(bean);


ExpressionParser parser = new SpelExpressionParser();

// Approach 1
Expression expr1 = parser.parseExpression("num1");
expr1.setValue(context, 10);

// Approach 2
Expression expr2 = parser.parseExpression("num2 = 20");
expr2.getValue(context);

System.out.println("NUM1 : " + bean.getNum1());


System.out.println("NUM2 : " + bean.getNum2());
System.out.println("ADD : " + bean.add());
System.out.println("SUB : " + bean.sub());
System.out.println("MUL : " + bean.mul());
}
}

SpEL features:

1. Expression
2. Operators
3. Variables
4. Method Invocations
5. Collections

You might also like