Advertisement



< 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 -



Utility.java
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.
		parser.parseRaw("#this.setName('Emp1')").getValue(stContext);
		
		//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.
		parser.parseRaw("#this.setTelNo('1111111')").getValue(stContext);
				
		//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.
		parser.parseRaw("#this.setPositionGrade('A')").getValue(stContext);
						
		//Accessing the getPositionGrade() method using #this reference.
		System.out.println("Employee's position Grade : " + parser.parseRaw("#this.getPositionGrade()").getValue(stContext));
			
			
			
		//Accessing the setYearsOfExp() method using #this reference.
		parser.parseRaw("#this.setYearsOfExp('6.2')").getValue(stContext);
								
		//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.


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.






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.






Execution


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 position 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 -




< Prev
Next >
< SpEl Operators
SpEL with a List >



Advertisement

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

C#, PYTHON and DJANGO


Advertisement