< Prev
Next >



SpEL Variables



In this tutorial, we will explain how Spring Expression Language(SpEL) allows us to define new variables, which could be used to set values of the properties associated with an object of a class. In order to define such variables :


Let's see an example explaining how to define some new variables and use their values to set the values of the properties of a class, using Spring Expression Language(SpEL).




Creating a Java Class - Employee


We are going to create a Java class named Employee within the decodejava package and this class is going to contain a few properties, such as - 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 static String getMessage() 
	{
		return message;
	}

	public static void setMessage(String message) 
	{
		Employee.message = message;
	}

	
	public char getPositionGrade() 
	{
		return positionGrade;
	}

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

	public float getYearsOfExp() 
	{
		return yearsOfExp;
	}

	public void setYearsOfExp(float yearsOfExp) 
	{
		this.yearsOfExp = yearsOfExp;
	}
	
	
	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;
	}
}





Adding the Utility class that calls the Spring API


Next, we are going to create another java class named - Utility.

Utility.java
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 Utility {

	public static void main(String[] args) 
	{
		Employee employee = new Employee();
		
		StandardEvaluationContext  stContext  = new StandardEvaluationContext(employee);
		stContext.setVariable("EmpName", "Employee1");
		stContext.setVariable("EmpTelephoneNo", 1111111);
		stContext.setVariable("EmpPositionGrade", "A");
		stContext.setVariable("EmpExperience", 6.7);
		stContext.setVariable("EmpMessage", "Best of luck!");
		
		SpelExpressionParser parser = new SpelExpressionParser();
		
		SpelExpression expression = parser.parseRaw("name=#EmpName");
		expression.getValue(stContext);
		System.out.println("Employee's Name :  " + employee.getName());
		
		
		expression = parser.parseRaw("telNo=#EmpTelephoneNo");
		expression.getValue(stContext);
		System.out.println("Employee's Telephone number : " + employee.getTelNo());	
		
		
		expression = parser.parseRaw("positionGrade=#EmpPositionGrade");
		expression.getValue(stContext);
		System.out.println("Employee's Position Grade :  " + employee.getPositionGrade());
		
		
		expression = parser.parseRaw("yearsOfExp=#EmpExperience");
		expression.getValue(stContext);
		System.out.println("Employee's Years Of Exp : " + employee.getYearsOfExp());
		
		
		expression = parser.parseRaw("message=#EmpMessage");
		expression.getValue(stContext);
		System.out.println("Message for employee : " + employee.getMessage());
	
	}
}


In this class we have first created an object of Employee class and performed the next few steps -





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

Employee's Name :  Employee1
Employee's Telephone number : 1111111
Employee's Position Grade :  A
Employee's Years Of Exp : 6.7
Message for employee : Best of luck!

This output shown below, shows the values of properties associated with an object of Employee class. These properties were set with the values of variables created using Spring Expression Language(SpEL).




Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Spring Expression Language(SpEL)
SpEL Operators >

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



COMING SOON
-
PYTHON