< Prev
Next >

SpEL to access methods

In this tutorial, we will explain how to access methods(static and non-static) of a class using the Spring Expression Language(SpEL). In order to access methods of a class, we are going to use #this reference, which allows us to access the currently executing object. It is similar to this keyword of Java, except that #this keyword of Spring Expression Language(SpEL) is preceded by a #.

Creating a Java Class - Employee

To display how to access and call the methods of a class using Spring Expression Language(SpEL) we are going to create a Java class named Employee within the decodejava package. and this class is going to contain a few properties - Besides this, we are also going to define a couple of getter and setter methods for these properties.

package decodejava;

public class Employee 
	private String name;
	private int telNo;
	private char positionGrade;
	private float yearsOfExp;
	static String message;
	public float getYearsOfExp() 
		return yearsOfExp;

	public void setYearsOfExp(float yearsOfExp) 
		this.yearsOfExp = yearsOfExp;

	public char getPositionGrade() 
		return positionGrade;

	public void setPositionGrade(char positionGrade) 
		this.positionGrade = positionGrade;

	public String getName() 
		return name;
	public void setName(String name) 
		this.name = name;
	public int getTelNo() {
		return telNo;
	public void setTelNo(int telNo) 
		this.telNo = telNo;
	public static String getMessage()
		return message;
	public static void setMessage(String m)
		message = m;

Adding the Utility class that calls the Spring API

Next, we are going to create another java class named - Utility. In this class we are going to create an object of Employee class and perform the next few steps -

package decodejava;

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

public class Utility {

	public static void main(String[] args) throws NoSuchMethodException, SecurityException 
		Employee employee = new Employee();
		StandardEvaluationContext  stContext  = new StandardEvaluationContext(employee);
		SpelExpressionParser parser = new SpelExpressionParser();
		//Accessing the setName() method using #this reference.
		//Accessing the getName() method using #this reference.
		System.out.println("Employee's Name : " + parser.parseRaw("#this.getName()").getValue(stContext));
		//Accessing the setTelNo() method using #this reference.
		//Accessing the getName() method using #this reference.
		System.out.println("Employee's Tel No : " + parser.parseRaw("#this.getTelNo()").getValue(stContext));
		//Accessing the setPositionGrade() method using #this reference.
		//Accessing the getPositionGrade() method using #this reference.
		System.out.println("Employee's Positon Grade : " + parser.parseRaw("#this.getPositionGrade()").getValue(stContext));
		//Accessing the setYearsOfExp() method using #this reference.
		//Accessing the getYearsOfExp() method using #this reference.
		System.out.println("Employee's Years of Exp : " + parser.parseRaw("#this.getYearsOfExp()").getValue(stContext));
		//Accessing the static method setMessage() method using #this reference
		System.out.println("Message is : " + parser.parseRaw("#this.setMessage('Hello')").getValue(stContext));
		//Accessing the static method getMessage() method using #this reference.
		System.out.println("Message is : " + parser.parseRaw("#this.getMessage()").getValue(stContext));


Note : Just like this reference of Java, #this reference of Spring Expression Language(SpEL) also refers to the currently executing object and when we use #this reference to call a static method of a class, Java compiler automatically replaces it with the its class name to call the static method.

Adding JARs

Directory Structure of Project

The picture above depicts how and where to arrange classes and interfaces comprising this Spring Project, in a specific directory structure.

Project Folder - SpELAccessingMethods is the name of our Project and it is a top-level directory.


Finally, after executing Utility class, you will get the following output within the Console window. This output shown below, shows how the Utility class has used #this keyword which refers to the current object of Employee class and have called the methods of Employee class on it.

Employee's Name : Emp1
Employee's Tel No : 1111111
Employee's Positon Grade : A
Employee's Years of Exp : 6.2
Message is : Hello

This concludes the tutorial of how to access methods(static and non-static) of a class using Spring Expression Language(SpEL) and its #this keyword.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< SpEl Operators
SpEL with a List >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next