Spring SpEL – Logical Operators ”; Previous Next SpEL expression supports logical operators like AND, OR and NOT. 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 java.util.HashMap; import java.util.Map; 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(); // evaluates to true boolean result = parser.parseExpression(“true and true”).getValue(Boolean.class); System.out.println(result); // evaluates to true result = parser.parseExpression(“true or false”).getValue(Boolean.class); System.out.println(result); // evaluates to false result = parser.parseExpression(“!true”).getValue(Boolean.class); System.out.println(result); } } Output true true false Print Page Previous Next Advertisements ”;
Category: spring Expression Language
Spring SpEL – Safe Navigation Operator ”; Previous Next SpEL expression supports Safe Navigation operator which is used to avoid NullPointerException. int length = parser.parseExpression(“name?.length”).getValue(context, Integer.class); Here if name is null, then expression will not throw null pointer exception. 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; public class Employee { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Here is the content of MainApp.java file − package com.tutorialspoint; import java.text.ParseException; 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(); EvaluationContext employeeContext = new StandardEvaluationContext(employee); // Evaluates to null but will not throw null pointer exception during parseExpression String result = parser.parseExpression(“name?.strip()”).getValue(employeeContext, String.class); System.out.println(result); employee.setName(” Mahesh “); // Evaluates to “Mahesh” result = parser.parseExpression(“name?.strip()”).getValue(employeeContext, String.class); System.out.println(result); } } Output null Mahesh Print Page Previous Next Advertisements ”;
Spring SpEL – Literal Expression ”; Previous Next SpEL expression supports following types of literals − Strings − Single quote delimited strings. To use single quote, put another single quote around it. Numeric − int, real and hex expressions are supported. boolean null 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 org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; public class MainApp { public static void main(String[] args) { ExpressionParser parser = new SpelExpressionParser(); // parse a simple text String message = (String) parser.parseExpression(“”Tutorialspoint””).getValue(); System.out.println(message); // parse a double from exponential expression double avogadros = (Double) parser.parseExpression(“6.0221415E+23”).getValue(); System.out.println(avogadros); // parse an int value from Hexadecimal expression int intValue = (Integer) parser.parseExpression(“0x7FFFFFFF”).getValue(); System.out.println(intValue); // parse a boolean boolean booleanValue = (Boolean) parser.parseExpression(“true”).getValue(); System.out.println(booleanValue); // parse a null object Object nullValue = parser.parseExpression(“null”).getValue(); System.out.println(nullValue); } } Output Tutorialspoint 6.0221415E23 2147483647 true null Print Page Previous Next Advertisements ”;
Spring SpEL – Annotation Based Configuration ”; Previous Next SpEL expression can be used in Annotation based beans configuration Syntax Following is an example of using an expression in annotation based configuration. @Value(“#{ T(java.lang.Math).random() * 100.0 }”) private int id; Here we are using @Value annotation and we”ve specified a SpEL expression on a property. Similarly we can specify SpEL expression on setter methods, on constructors and during autowiring as well. @Value(“#{ systemProperties[”user.country”] }”) public void setCountry(String country) { this.country = country; } 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 − An employee class. AppConfig.java − A configuration class. MainApp.java − Main application to run and test. Here is the content of Employee.java file − package com.tutorialspoint; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Employee { @Value(“#{ T(java.lang.Math).random() * 100.0 }”) 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; } @Value(“Mahesh”) public void setName(String name) { this.name = name; } public String getCountry() { return country; } @Value(“#{ systemProperties[”user.country”] }”) public void setCountry(String country) { this.country = country; } @Override public String toString() { return “[” + id + “, ” + name + “, ” + country + “]”; } } Here is the content of AppConfig.java file − package com.tutorialspoint; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = “com.tutorialspoint”) public class AppConfig { } Here is the content of MainApp.java file − package com.tutorialspoint; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MainApp { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(AppConfig.class); context.refresh(); Employee emp = context.getBean(Employee.class); System.out.println(emp); } } Output [84, Mahesh, IN] Print Page Previous Next Advertisements ”;
Spring SpEL – Useful Resources ”; Previous Next The following resources contain additional information on Spring SpEL. Please use them to get more in-depth knowledge on this topic. Useful Links on Spring SpEL SpEL −Spring Expression Language Documentation. Spring Source − Find latest news about Spring Framework, download section and all about Spring. Spring Framework Documentation − Complete Spring Framework reference covering all the modules. Oracle”s Site on JDBC − Sun Developer Network giving link on JDBC material. MySQL Connector/J − MySQL Connector/J is the official JDBC driver for MySQL. The JavaTM Tutorials − The Java Tutorials are practical guides for programmers who want to use the Java programming language to create applications. JavaTM 2 SDK, Standard Edition − Official site for JavaTM 2 SDK, Standard Edition Free Java Download − Download Java for your desktop computer now! Java Technology Reference − Sun Microsystem”s official website listing down all the API documentation, latest Java Technologies, Books and other resource. To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;
Spring SpEL – Elvis Operator
Spring SpEL – Elvis Operator ”; Previous Next SpEL expression supports Elvis operator which is a short form of ternary operator. // Using ternary operator String result = name != null ? name: “unknown”; // Using Elvis Operator result = name?:”unknown”; 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; public class Employee { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Here is the content of MainApp.java file − package com.tutorialspoint; import java.text.ParseException; 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(); EvaluationContext employeeContext = new StandardEvaluationContext(employee); // Evaluates to “unknown” String result = parser.parseExpression(“name?:”unknown””).getValue(employeeContext, String.class); System.out.println(result); employee.setName(“Mahesh”); // Evaluates to “Mahesh” result = parser.parseExpression(“name?:”unknown””).getValue(employeeContext, String.class); System.out.println(result); } } Output unknown Mahesh Print Page Previous Next Advertisements ”;
Spring SpEL – Collection Selection ”; Previous Next SpEL expression supports Collection Selection which is a very powerful expression allowing to transform source collection into another by selecting the entries from the source collection. Syntax ?[selectionExpresion] Following example shows the usage. List<Employee> list = (List<Employee>) parser.parseExpression(“employees.?[country == ”USA”]”).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 employees who are living in USA List<Employee> list = (List<Employee>) parser.parseExpression(“employees.?[country == ”USA”]”).getValue(deptContext); System.out.println(list); } } Output [[Robert, USA], [Julie, USA]] Print Page Previous Next Advertisements ”;
Spring SpEL – Variables
Spring SpEL – Variables ”; Previous Next SpEL expression allows to create and use variables specific to expression using #variable-name syntax. A variable is set using setVariable on EvaluationContext. There are two types of inbuilt variables as well, #this and #root. #this variable always refers to current evaluation object where as #root variable refers to the root object of the evaluation context. Syntax context.setVariable(“newName”, “Mahesh Kumar”); 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; 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 MainApp.java file − package com.tutorialspoint; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; 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 employee = new Employee(“Mahesh”, “INDIA”); EvaluationContext context = new StandardEvaluationContext(employee); context.setVariable(“newName”, “Mahesh Parashar”); parser.parseExpression(“Name = #newName”).getValue(context); // Evaluate to “Mahesh Parashar” System.out.println(employee.getName()); List<Integer> primes = new ArrayList<Integer>(); primes.addAll(Arrays.asList(2,3,5,7,11,13,17)); context.setVariable(“primes”,primes); List<Integer> filteredList = (List<Integer>) parser.parseExpression(“#primes.?[#this>10]”).getValue(context); // Evaluate to [11, 13, 17], prime numbers greater than 10 System.out.println(filteredList); } } Output Mahesh Parashar [11, 13, 17] Print Page Previous Next Advertisements ”;
Spring SpEL – Ternary Operator ”; Previous Next SpEL expression supports ternary operator to perform if-then-else logic. 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.spel.standard.SpelExpressionParser; public class MainApp { public static void main(String[] args) throws ParseException { ExpressionParser parser = new SpelExpressionParser(); String result = parser.parseExpression(“true ? ”Yes” : ”No””).getValue(String.class); System.out.println(result); result = parser.parseExpression(“false ? ”Yes” : ”No””).getValue(String.class); System.out.println(result); } } Output Yes No Print Page Previous Next Advertisements ”;
Spring SpEL – Mathematical Operators ”; Previous Next SpEL expression supports mathematical operators like +, -, * etc. 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.spel.standard.SpelExpressionParser; public class MainApp { public static void main(String[] args) throws ParseException { ExpressionParser parser = new SpelExpressionParser(); // evaluates to 5 int result = parser.parseExpression(“3 + 2”).getValue(Integer.class); System.out.println(result); // evaluates to 1 result = parser.parseExpression(“3 – 2”).getValue(Integer.class); System.out.println(result); // evaluates to 6 result = parser.parseExpression(“3 * 2”).getValue(Integer.class); System.out.println(result); // evaluates to 1 result = parser.parseExpression(“3 / 2”).getValue(Integer.class); System.out.println(result); // evaluates to 1 result = parser.parseExpression(“3 % 2”).getValue(Integer.class); System.out.println(result); // follow operator precedence, evaluate to -9 result = parser.parseExpression(“1+2-3*4″).getValue(Integer.class); System.out.println(result); } } Output 5 1 6 1 1 -9 Print Page Previous Next Advertisements ”;