Nearby lessons
13 of 35Spring - SpEL (Spring Expression Language)
- Understand Spring - SpEL (Spring Expression Language)
- See working code examples
- Learn from common mistakes and Q&A
Learn Spring - SpEL (Spring Expression Language) step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.
EXample with StandardEvaluationContext
CalculatorBean.java
package com.durgasoft.beans;
public class CalculatorBean {
private int num1;
private int num2;
setXXX() and getXXX()
public int add() {
return num1+num2;
}
public int sub() {
return num1-num2;
}
public int mul() {
return num1*num2;
}
}
Test.java
package com.durgasoft.test;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import com.durgasoft.beans.CalculatorBean;
public class Test {
public static void main(String[] args) {
CalculatorBean cal = new CalculatorBean();
StandardEvaluationContext context = new
StandardEvaluationContext(cal);
ExpressionParser parser = new SpelExpressionParser();
Expression expr1 = parser.parseExpression("num1");
expr1.setValue(context, "10");
Expression expr2 = parser.parseExpression("num2");
expr2.setValue(context, "5");
System.out.println("num1 :"+cal.getNum1());
System.out.println("num2 :"+cal.getNum2());
System.out.println("ADD :"+cal.add());
System.out.println("SUB :"+cal.sub());
System.out.println("MUL :"+cal.mul());
}
}
SpEL Features
1.Expressions
2.Operators
3.Variables
4.Medthod Invocations
5.Collections
1.Expressions
SpEL is able to allow two types of Expressions.
1.Literal Expressions
2.Regular Expressions
1.Literal Expressions
---> It able to allow only literals inside the Expressions.
2.Regular Expressions
---> It able to check the specified String against the provided Regular Expressions, If the
provided String is as per the provided Regular Expression then it will return true value
otherwise it will return false value.
To compare String with Regular Expression we have to use "matches" operator, it is a
boolean operator, it will check whether the provided String is satisifying the provided
Regular Expression or not.
Syntax:
'String_Data' matches 'Reg_Expression'
Example:
package com.durgasoft.test;
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)throws Exception {
ExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("10 + 10");
System.out.println(expr.getValue());
expr = parser.parseExpression("'abc' + 'def'");
System.out.println(expr.getValue());
expr = parser.parseExpression("0xABCDEF*10+6");
System.out.println(expr.getValue());
expr = parser.parseExpression("'Spring' matches 'Sp.*'");
System.out.println(expr.getValue());
expr = parser.parseExpression("'abc@durgasoft.com' matches '[a-
z]*@durgasoft.com'");
System.out.println(expr.getValue());
expr = parser.parseExpression("'abc@gmail.com' matches '[a-
z]*@durgasoft.com'");
System.out.println(expr.getValue());
}
}
2.Operators:
In SpEL, to prepare Expressions we are able to use the following operators.
i).Arithmetic Operators:
+, -, *, /, %,.....
ii).Logical Operators:
and or &&
or or ||
not or !
iii).Comparision Operators:
eq or ==
ne or !=
lt or <
le or <=
gt or >
ge or >=
iv).Ternary Operator:
Expr1? Expr2: Expr3;
v).Type Operator[T]:
It able to represent a particular class type or interface type in SpEL.
Syntax:
vi).Safe Navigation Operator:
In general, in JAVA applications, if we access any instance variable or method on a
reference variable contains null value then JVM will rise an exception like
java.lang.NullPointerException. In SpEL , to avoid NullPointerExceptions we are able to
use "Safe Navigation" operator.
Syntax:
var_Name?.methid_or_Var_Name()
Example:
package com.durgasoft.beans;
public class User {
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
}
Test.java
package com.durgasoft.test;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import com.durgasoft.beans.User;
public class Test {
public static void main(String[] args)throws Exception {
ExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("10 + 20");
System.out.println(expr.getValue());
expr = parser.parseExpression("10 * 20");
System.out.println(expr.getValue());
expr = parser.parseExpression("true and true");
System.out.println(expr.getValue());
expr = parser.parseExpression("true && false");
System.out.println(expr.getValue());
expr = parser.parseExpression("true or true");
System.out.println(expr.getValue());
expr = parser.parseExpression("true || false");
System.out.println(expr.getValue());
expr = parser.parseExpression("10 ne 20");
System.out.println(expr.getValue());
expr = parser.parseExpression("10 >= 20");
System.out.println(expr.getValue());
expr = parser.parseExpression("10 lt 5");
System.out.println(expr.getValue());
expr = parser.parseExpression("10 ge 5");
System.out.println(expr.getValue());
expr = parser.parseExpression("10 eq 10? 'condition is true': 'condition is
System.out.println(expr.getValue());
expr = parser.parseExpression("T(Thread).MIN_PRIORITY");
System.out.println(expr.getValue());
expr = parser.parseExpression("T(Integer).toString(10)");
System.out.println(expr.getValue());
User u = new User();
StandardEvaluationContext context = new StandardEvaluationContext(u);
expr = parser.parseExpression("uname?.toUpperCase()");
System.out.println(expr.getValue(context));
}
}
3.Variables
In SpEL, if we want to access any variable which is already existed in
StandardEvaluationContext then we have to use the following syntax.
#var_Name.
If we want to set variable to StandardEvaluationContext then we have to use the
following method.
public void setVariable(String var_Name, Object val)
Example:
MyMath.java
package com.durgasoft.beans;
public class MyMath {
private int num1;
private int num2;
setXXX() and getXXX()
public int add() {
return num1+num2;
}
public int sub() {
return num1-num2;
}
public int mul() {
return num1*num2;
}
}
Test.java
package com.durgasoft.test;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import com.durgasoft.beans.MyMath;
public class Test {
public static void main(String[] args)throws Exception {
MyMath math = new MyMath();
StandardEvaluationContext context = new
StandardEvaluationContext(math);
context.setVariable("number1", 10);
context.setVariable("number2", 5);
ExpressionParser parser = new SpelExpressionParser();
Expression expr1 = parser.parseExpression("num1 = #number1");
Expression expr2 = parser.parseExpression("num2 = #number2");
System.out.println(math.add());
System.out.println(math.sub());
System.out.println(math.mul());
}
}
4.Medthod Invocations
In SpEL , we are able to declare methods and we are able to access that methods as per
the requirement.
If we want to declare an user defined method and if we want to access user defined
method through an expression then we have to use the following steps.
1.Create StandardEvaluationContext object.
2.Register Method with a name.
public void registerFunction(String meth_Name, Method m)
3.Prepare an Expression with method call and access it.
Example:
MyString.java
package com.durgasoft.beans;
public class MyString {
public static void reverseString(String str) {
StringBuffer sb = new StringBuffer(str);
System.out.println(sb.reverse());
}
}
Test.java
package com.durgasoft.test;
import java.lang.reflect.Method;
import org.springframework.expression.Expression;
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)throws Exception {
StandardEvaluationContext context = new StandardEvaluationContext();
Class cls = Class.forName("com.durgasoft.beans.MyString");
Method method = cls.getDeclaredMethod("reverseString", new
Class[]{java.lang.String.class});
context.registerFunction("reverse", method);
context.setVariable("str", "Durga Software Solutions");
ExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("#reverse(#str)");
expr.getValue(context);
// Accessing Predefined methods
expr = parser.parseExpression("new java.util.Date().toString()");
System.out.println(expr.getValue());
expr = parser.parseExpression("'Durga Software
Solutions'.toUpperCase()");
System.out.println(expr.getValue());
}
}
5.Collections
In SpEL, we are able to declare Collections and we are able to access the content from
Collection objects by using the following Expression Syntax.
Collection_Ref_Var.?Condition_Expression
Example:
City_State.java
package com.durgasoft.beans;
public class City_State {
private String city;
private String state;
public City_State(String city, String state) {
this.city = city;
this.state = state;
}
setXXX() and getXXX()
public String toString() {
return city+"="+state;
}
}
City_State_Collection.java
package com.durgasoft.beans;
import java.util.ArrayList;
public class City_State_Collection {
private ArrayList<City_State> city_State = new ArrayList<City_State>();
public ArrayList<City_State> getCity_State(){
city_State.add(new City_State("Hyd","Tel"));
city_State.add(new City_State("VJA","AP"));
city_State.add(new City_State("Warangal","Tel"));
city_State.add(new City_State("Vizag","AP"));
city_State.add(new City_State("KNGR","Tel"));
city_State.add(new City_State("TRPTI","AP"));
return city_State;
}
}
Test.java
package com.durgasoft.test;
import java.util.ArrayList;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import com.durgasoft.beans.City_State;
import com.durgasoft.beans.City_State_Collection;
public class Test {
public static void main(String[] args)throws Exception {
City_State_Collection collection = new City_State_Collection();
StandardEvaluationContext context = new
StandardEvaluationContext(collection);
ExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("city_State.?[state == 'AP']");
ArrayList<City_State> al = (ArrayList<City_State>)expr.getValue(context);
System.out.println(al);
}
}
{Spring JDBC
MODULE}
SPRING JDBC MODULE INDEX
- Spring-DAO.....................................................................................PAGE 238
- Spring JDBC.....................................................................................PAGE 256
- Spring JDBC Connection Pooling Mechanism..............................PAGE 301
Spring-DAO
-->DAO [Data Access Object], it is a design pattern, it able to provide very good environment to
separate Data Access logic from Business Processing logic.
-->In enterprise Applications, to prepare Data Access Layer we will use DAO Design pattern.
Advantages of DAOs in Enterprise Applications:
SpEL Features
tech. with out giving any effect to Service/Business Layer, that is from JDBC to Hibernate,....
SpEL Features
maintanance of Enterprise Applications.
SpEL Features
to prepare Service/Business layer.
SpEL Features
- Key ideas of Spring - SpEL (Spring Expression Language) explained simply
- Ready-to-use code examples
- Exam-style questions at the end