Advertisement



< Prev
Next >



Introducing Spring Expression Language(SpEL)



In this tutorial, we are going to introduce you to the concept of Spring Expression Language(SpEL). As the name says, Spring Expression Language(SpEL) is a an expression language provided by Spring Framework which allows us to write simple expressions by which we can access the classes, methods, objects and their properties.

These expressions to access classes, methods, objects(beans) and their properties can be written and placed within the code of a Java class or within the xml configuration file.




Important interfaces and classes in SpEL


To write simple expressions though which we could access classes, their methods and query their objects(beans) and access/set their properties, Spring Expression Language(SpEL) provides us some important interfaces and classes.

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.





SpEL expressions


We can write different kind of expressions in Spring Expression Language(SpEL). We could write expressions using literal values as int, float, double, String. We can write expressions involving different form of operators such as arithmetic, relational, logical etc. Spring Expression Language also provides us a matches keyword which allows us write expression which compares the values of two String objects with each other.

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




Adding JARs


  • We are going to add some JARs files to our Java project. These JARs are required in order to successfully execute a Spring project.

    All these JARs are included in the folder named libs(within our Spring installation folder). So, we need to add all the JARs in the libs folder to our build path of our Java project.

    Note : Those who don't know how to add JARs to the build path of your Java project in Eclipse IDE, please refer to our section Adding JARs to your Spring project in Eclipse.





Output
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





SpEL evaluating object


Now, we are going to explain how to use SpEL to evaluate object using StandardEvaluationContext class. This class evaluates an expression in the context of an object of a class. Evaluation an object allows us to call methods on this object and set/access its properties. Let's see how we do it with a coding example.

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());
	
	}
}


Output
Message for Employee : Welcome ID 1


Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement