Advertisement



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



Advertisement




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


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




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



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