Spring SpEL – Properties ”; Previous Next SpEL expression supports accessing properties of an object. We can access nested properties as well within an SpEL expression. First letter of a property is case insensitive within an SpEL expression. Following example shows the various use cases. Example Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding/updating following files − Employee.java − Employee class. MainApp.java − Main application to run and test. Here is the content of Employee.java file − package com.tutorialspoint; import java.util.Date; public class Employee { private int id; private String name; private Date dateOfBirth; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDateOfBirth() { return dateOfBirth; } public void setDateOfBirth(Date dateOfBirth) { this.dateOfBirth = dateOfBirth; } @Override public String toString() { return “[” + id + “, ” + name + “, ” + dateOfBirth + “]”; } } Here is the content of MainApp.java file − package com.tutorialspoint; import java.text.ParseException; import java.text.SimpleDateFormat; import org.springframework.expression.EvaluationContext; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class MainApp { public static void main(String[] args) throws ParseException { ExpressionParser parser = new SpelExpressionParser(); Employee employee = new Employee(); employee.setId(1); employee.setName(“Mahesh”); employee.setDateOfBirth(new SimpleDateFormat(“YYYY-MM-DD”).parse(“1985-12-01”)); EvaluationContext context = new StandardEvaluationContext(employee); int birthYear = (Integer) parser.parseExpression(“dateOfBirth.Year + 1900”).getValue(context); System.out.println(birthYear); String name = (String) parser.parseExpression(“name”).getValue(context); System.out.println(name); } } Output 1984 Mahesh Print Page Previous Next Advertisements ”;
Category: spring Expression Language
Spring SpEL – Expression Templating ”; Previous Next SpEL expression allows to mix literal text with evaluation block(s). Each evaluation block is to be prefixed and suffixed properly. Standard choice is to use #{}. org.springframework.expression.common. TemplateParserContextTemplateParserContext uses the same. Syntax String result = parser.parseExpression(“Random number : #{T(java.lang.Math).random() * 100}”, new TemplateParserContext()).getValue(String.class); Following example shows the various use cases. Example Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding/updating following files − MainApp.java − Main application to run and test. Here is the content of MainApp.java file − package com.tutorialspoint; import java.text.ParseException; import org.springframework.expression.ExpressionParser; import org.springframework.expression.common.TemplateParserContext; import org.springframework.expression.spel.standard.SpelExpressionParser; public class MainApp { public static void main(String[] args) throws ParseException, NoSuchMethodException, SecurityException { ExpressionParser parser = new SpelExpressionParser(); String result=parser.parseExpression(“Random number : #{T(java.lang.Math).random() * 100}”, new TemplateParserContext()).getValue(String.class); System.out.println(result); } } Output Random number : 18.056323318070998 Print Page Previous Next Advertisements ”;
Spring SpEL – Constructor
Spring SpEL – Constructor ”; Previous Next SpEL expression supports creating objects within expressions using new operator. We need to pass the fully qualified name of the class. Syntax Employee robert = parser.parseExpression(“new com.tutorialspoint.Employee(”Robert”,”USA”)”).getValue(Employee.class); Following example shows the various use cases. Example Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding/updating following files − Employee.java − Employee class. Dept.java − Department class. MainApp.java − Main application to run and test. Here is the content of Employee.java file − package com.tutorialspoint; public class Employee { private String name; private String country; public Employee(String name, String country) { this.name = name; this.country = country; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String toString() { return “[” +name+ “, “+country + “]”; } } Here is the content of Dept.java file − package com.tutorialspoint; import java.util.List; public class Dept { private List<Employee> employees; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } public String toString() { return “[” + employees.toString() + “]”; } } Here is the content of MainApp.java file − package com.tutorialspoint; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import org.springframework.expression.EvaluationContext; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class MainApp { public static void main(String[] args) throws ParseException { ExpressionParser parser = new SpelExpressionParser(); List<Employee> employees = new ArrayList<Employee>(); Employee robert = parser.parseExpression(“new com.tutorialspoint.Employee(”Robert”,”USA”)”) .getValue(Employee.class); employees.add(robert); Dept dept = new Dept(); dept.setEmployees(employees); System.out.println(dept); EvaluationContext deptContext = new StandardEvaluationContext(dept); parser.parseExpression(“employees.add(new com.tutorialspoint.Employee(”Julie”,”USA”))”) .getValue(deptContext); System.out.println(dept); } } Output [[[Robert, USA]]] [[[Robert, USA], [Julie, USA]]] Print Page Previous Next Advertisements ”;
Spring SpEL – XML Based Configuration ”; Previous Next SpEL expression can be used in XML based beans configuration Syntax Following is an example of using an expression in xml configuration. <bean id=”randomNumberGenerator” class=”com.tutorialspoint.RandomNumberGenerator”> <property name=”randomNumber” value=”#{ T(java.lang.Math).random() * 100.0 }”/> </bean> Here we have specified a property to be filled in using Math.random() method. In case of classes, its name should be fully qualified. We can use system variables as well using systemProperties. It is a built-in variable. <property name=”country” value=”#{ systemProperties[”user.country”] }”/> We can use another bean as well with a SpEL expression as shown below: <property name=”id” value=”#{ randomNumberGenerator.randomNumber }”/> Following example shows the various use cases. Example Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding/updating following files − RandomNumberGenerator.java − A random number generator class. Employee.java − An employee class. MainApp.java − Main application to run and test. applicationcontext.xml − beans configuration file. Here is the content of RandomNumberGenerator.java file − package com.tutorialspoint; public class RandomNumberGenerator { private int randomNumber; public int getRandomNumber() { return randomNumber; } public void setRandomNumber(int randomNumber) { this.randomNumber = randomNumber; } } Here is the content of Employee.java file − package com.tutorialspoint; public class Employee { private int id; private String name; private String country; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } @Override public String toString() { return “[” + id + “, ” + name + “, ” + country + “]”; } } Here is the content of MainApp.java file − package com.tutorialspoint; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“applicationcontext.xml”); Employee employee = (Employee) applicationContext.getBean(“employee”); System.out.println(employee); } } Here is the content of applicationcontext.xml file − <?xml version=”1.0″ encoding=”UTF-8″?> <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”> <bean id=”randomNumberGenerator” class=”com.tutorialspoint.RandomNumberGenerator”> <property name=”randomNumber” value=”#{ T(java.lang.Math).random() * 100.0 }”/> </bean> <bean id=”employee” class=”com.tutorialspoint.Employee”> <property name=”id” value=”#{ randomNumberGenerator.randomNumber }”/> <property name=”country” value=”#{ systemProperties[”user.country”] }”/> <property name=”name” value=”Mahesh”/> </bean> </beans> Output [84, Mahesh, IN] Print Page Previous Next Advertisements ”;
Spring SpEL – List
Spring SpEL – List ”; Previous Next SpEL expression supports accessing list and using their indexes of an list of an object. We can access nested lists as well within an SpEL expression. Following example shows the various use cases. Example Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding/updating following files − Employee.java − Employee class. Dept.java − Department class. MainApp.java − Main application to run and test. Here is the content of Employee.java file − package com.tutorialspoint; public class Employee { private List<String> awards; public List<String> getAwards() { return awards; } public void setAwards(List<String> awards) { this.awards = awards; } } Here is the content of Dept.java file − package com.tutorialspoint; public class Dept { private List<Employee> employees; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } Here is the content of MainApp.java file − package com.tutorialspoint; import java.text.ParseException; import java.util.Arrays; import org.springframework.expression.EvaluationContext; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class MainApp { public static void main(String[] args) throws ParseException { ExpressionParser parser = new SpelExpressionParser(); Employee employee = new Employee(); employee.setAwards(Arrays.asList(“Star of the Month”, “Champion”, “Accelerator”)); Dept dept = new Dept(); dept.setEmployees(Arrays.asList(employee)); EvaluationContext employeeContext = new StandardEvaluationContext(employee); // evaluates to “Accelerator” String award = parser.parseExpression(“awards.get(2)”).getValue(employeeContext, String.class); System.out.println(award); EvaluationContext deptContext = new StandardEvaluationContext(dept); // evaluates to “Champion” award = parser.parseExpression(“employees.get(0).awards.get(1)”).getValue(deptContext, String.class); System.out.println(award); } } Output Accelerator Champion Print Page Previous Next Advertisements ”;
Spring SpEL – Collection Projection ”; Previous Next SpEL expression supports Collection Projection which is a very powerful expression allowing to evaluate sub-expression and in result returns a new collection. Syntax ![projectionExpresion] Following example shows the usage. List<String> list = (List<String>) parser.parseExpression(“employees.![country]”).getValue(deptContext); Here SpEL will return only those employees from the list of employees whose country is USA. Following example shows the various use cases. Example Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding/updating following files − Employee.java − Employee class. Dept.java − Department class. MainApp.java − Main application to run and test. Here is the content of Employee.java file − package com.tutorialspoint; public class Employee { private String name; private String country; public Employee(String name, String country) { this.name = name; this.country = country; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String toString() { return “[” +name+ “, “+country + “]”; } } Here is the content of Dept.java file − package com.tutorialspoint; import java.util.List; public class Dept { private List<Employee> employees; public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } } Here is the content of MainApp.java file − package com.tutorialspoint; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import org.springframework.expression.EvaluationContext; import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class MainApp { public static void main(String[] args) throws ParseException { ExpressionParser parser = new SpelExpressionParser(); Employee employee1 = new Employee(“Robert”, “USA”); Employee employee2 = new Employee(“Julie”, “USA”); Employee employee3 = new Employee(“Ramesh”, “India”); List<Employee> employees = new ArrayList<Employee>(); employees.add(employee1); employees.add(employee2); employees.add(employee3); Dept dept = new Dept(); dept.setEmployees(employees); EvaluationContext deptContext = new StandardEvaluationContext(dept); // Select list of countries List<String> list = (List<String>) parser.parseExpression(“employees.![country]”).getValue(deptContext); System.out.println(list); } } Output [USA, USA, India] Print Page Previous Next Advertisements ”;
Spring SpEL – Home
Spring SpEL Tutorial PDF Version Quick Guide Resources Job Search Discussion The Spring Expression Language, SpEL is a very powerful expression language and it supports querying and manipulating an object graph at runtime. It offers many advanced features like method invocation and basic string templating functionality. Spring Expression Language was originally created for the Spring community to have a single well supported expression language to be used across all the products in the Spring portfolio. While SpEL serves as the foundation for expression evaluation within the Spring portfolio, it is not directly tied to Spring and can be used independently. In this tutorial, we”ll cover Spring Expression Language, SpEL in Spring framework which helps in solving the common problems developers/users face in spring framework based applications. Audience This tutorial is designed for Java programmers with a need to understand the Spring framework in detail along with its architecture and actual usage. This tutorial will bring you at an intermediate level of expertise, from where you can take yourself to higher levels of expertise. Prerequisites Before proceeding with this tutorial, you should have a good understanding of Java programming language. Print Page Previous Next Advertisements ”;