Spring SpEL – Map ”; Previous Next SpEL expression supports accessing map. 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.Map; public class Employee { private Map<String, String> offices; public Map<String, String> getOffices() { return offices; } public void setOffices(Map<String, String> offices) { this.offices = offices; } } 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(); Employee employee = new Employee(); Map<String, String> officeMap = new HashMap(); officeMap.put(“IN”, “Hyderabad”); officeMap.put(“UK”, “London”); employee.setOffices(officeMap); EvaluationContext employeeContext = new StandardEvaluationContext(employee); // evaluates to “Hyderabad” String city = parser.parseExpression(“offices[”IN”]”).getValue(employeeContext, String.class); System.out.println(city); } } Output Hyderabad Print Page Previous Next Advertisements ”;
Category: spring Expression Language
Spring SpEL – Methods
Spring SpEL – Methods ”; Previous Next SpEL expression supports accessing methods of an object. 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 List<String> awards; public List<String> getAwards() { return awards; } public void setAwards(List<String> awards) { this.awards = awards; } public boolean isAwardee() { return awards != null && awards.size() > 0; } } 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”)); EvaluationContext employeeContext = new StandardEvaluationContext(employee); // string literal, evaluates to “t” String t = parser.parseExpression(“”Tutorials”.substring(2, 3)”).getValue(String.class); System.out.println(t); // evaluates to true boolean isAwardee = parser.parseExpression(“isAwardee()”).getValue(employeeContext, Boolean.class); System.out.println(isAwardee); } } Output t true Print Page Previous Next Advertisements ”;
Spring SpEL – Relational Operators ”; Previous Next SpEL expression supports relational operators like <, >, equals etc. It also support instance of and matches operators. 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(“2 == 2”).getValue(Boolean.class); System.out.println(result); // evaluates to false result = parser.parseExpression(“2 < -5.0”).getValue(Boolean.class); System.out.println(result); // evaluates to true result = parser.parseExpression(“”black” < ”block””).getValue(Boolean.class); System.out.println(result); // evaluates to false result = parser.parseExpression(“”xyz” instanceof T(int)”).getValue(Boolean.class); System.out.println(result); // evaluates to false result = parser.parseExpression(“”5.0067” matches ”^-?\d+(\.\d{2})?$””).getValue(Boolean.class); System.out.println(result); } } Output true false true false false Print Page Previous Next Advertisements ”;
Spring SpEL – Expression Interface ”; Previous Next ExpressionParser is the main interface of Spring SpEL which helps parsing expression strings into compiled expressions. These compiled expressions can be evaluated and supports parsing templates as well as standard expression string. Syntax Following is an example of creating an ExpressionParser and using its object to get a value. ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression(“”Welcome to Tutorialspoint””); String message = (String) exp.getValue(); It should print the result as follows − Welcome to Tutorialspoint ExpressionParser − An interface responsible to parse an expression string. Expression − An interface responsible to evaluate an expression string. Exceptions − ParseException and EvaluationException can be thrown during parseExpression and getValue method invokation. SpEL supports calling methods, calling constructors and accessing properties. Following example shows the various use cases. Example The following example shows a class MainApp. Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding 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.Expression; 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 plain text Expression exp = parser.parseExpression(“”Welcome to tutorialspoint””); String message = (String) exp.getValue(); System.out.println(message); // invoke a method exp = parser.parseExpression(“”Welcome to tutorialspoint”.concat(”!”)”); message = (String) exp.getValue(); System.out.println(message); // get a property exp = parser.parseExpression(“”Welcome to tutorialspoint”.bytes”); byte[] bytes = (byte[]) exp.getValue(); System.out.println(bytes.length); // get nested properties exp = parser.parseExpression(“”Welcome to tutorialspoint”.bytes.length”); int length = (Integer) exp.getValue(); System.out.println(length); //Calling constructor exp = parser.parseExpression(“new String(”Welcome to tutorialspoint”).toUpperCase()”); message = (String) exp.getValue(); System.out.println(message); } } Output Welcome to tutorialspoint Welcome to tutorialspoint! 25 25 WELCOME TO TUTORIALSPOINT Print Page Previous Next Advertisements ”;
Spring SpEL – Array
Spring SpEL – Array ”; Previous Next SpEL expression supports accessing arrays and using their indexes of an array of an object. We can access nested arrays 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 String[] awards; public String[] getAwards() { return awards; } public void setAwards(String[] awards) { this.awards = awards; } } Here is the content of Dept.java file − package com.tutorialspoint; public class Dept { private Employee[] employees; public Employee[] getEmployees() { return employees; } public void setEmployees(Employee[] employees) { this.employees = employees; } } 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(); String[] awards = {“Star of the Month”, “Champion”, “Accelerator”}; employee.setAwards(awards); Employee[] employees = { employee }; Dept dept = new Dept(); dept.setEmployees(employees); EvaluationContext employeeContext = new StandardEvaluationContext(employee); // evaluates to “Accelerator” String award = parser.parseExpression(“awards[2]”).getValue(employeeContext, String.class); System.out.println(award); EvaluationContext deptContext = new StandardEvaluationContext(dept); // evaluates to “Champion” award = parser.parseExpression(“employees[0].awards[1]”).getValue(deptContext, String.class); System.out.println(award); } } Output Accelerator Champion Print Page Previous Next Advertisements ”;
Spring SpEL – EvaluationContext ”; Previous Next EvaluationContext is an interface of Spring SpEL which helps to execute an expression string in a context. References are resolved in this context when encountered during expression evaluation. Syntax Following is an example of creating an EvaluationContext and using its object to get a value. ExpressionParser parser = new SpelExpressionParser(); Expression exp = parser.parseExpression(“”name””); EvaluationContext context = new StandardEvaluationContext(employee); String name = (String) exp.getValue(); It should print the result as follows: Mahesh Here the result is the value of the name field of the employee object, Mahesh. The StandardEvaluationContext class specifies the object against which the expression is evaluated. StandardEvaluationContext cannot be changed once context object is created. It caches the state and allows expression evaluation to be performed quickly. Following example shows the various usecases. Example The following example shows a class MainApp. Let”s update the project created in Spring SpEL – Create Project chapter. We”re adding 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 id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } 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 org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; 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) { Employee employee = new Employee(); employee.setId(1); employee.setName(“Mahesh”); ExpressionParser parser = new SpelExpressionParser(); EvaluationContext context = new StandardEvaluationContext(employee); Expression exp = parser.parseExpression(“name”); // evaluate object using context String name = (String) exp.getValue(context); System.out.println(name); Employee employee1 = new Employee(); employee1.setId(2); employee1.setName(“Rita”); // evaluate object directly name = (String) exp.getValue(employee1); System.out.println(name); exp = parser.parseExpression(“id > 1″); // evaluate object using context boolean result = exp.getValue(context, Boolean.class); System.out.println(result); // evaluates to false result = exp.getValue(employee1, Boolean.class); System.out.println(result); // evaluates to true } } Output Mahesh Rita false true Print Page Previous Next Advertisements ”;
Spring SpEL – Environment Setup ”; Previous Next This chapter will guide you on how to prepare a development environment to start your work with Spring Framework. It will also teach you how to set up JDK, Maven and Eclipse on your machine before you set up Spring Framework − Setup Java Development Kit (JDK) You can download the latest version of SDK from Oracle”s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively. If you are running Windows and have installed the JDK in C:jdk-11.0.11, you would have to put the following line in your C:autoexec.bat file. set PATH=C:jdk-11.0.11;%PATH% set JAVA_HOME=C:jdk-11.0.11 Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button. On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-11.0.11 and you use the C shell, you will have to put the following into your .cshrc file. setenv PATH /usr/local/jdk-11.0.11/bin:$PATH setenv JAVA_HOME /usr/local/jdk-11.0.11 Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE. Setup Eclipse IDE All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine. To install Eclipse IDE, download the latest Eclipse binaries from www.eclipse.org/downloads. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe %C:eclipseeclipse.exe Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine − $/usr/local/eclipse/eclipse After a successful startup, if everything is fine then it should display the following result − Set Maven In this tutorial, we are using maven to run and build the spring based examples. Follow the Maven – Environment Setup to install maven. We”ll cover each and every topic in next chapters. Print Page Previous Next Advertisements ”;
Spring SpEL – Functions
Spring SpEL – Functions ”; Previous Next SpEL expression allows to create and use functions specific to expression using #function-name syntax. A function is set using registerFunction on EvaluationContext. Syntax context.registerFunction(“reverse”, MainApp.class.getDeclaredMethod(“reverse”, new Class[] { 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.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; public class MainApp { public static void main(String[] args) throws ParseException, NoSuchMethodException, SecurityException { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext context = new StandardEvaluationContext(); context.registerFunction(“reverse”, MainApp.class.getDeclaredMethod(“reverse”, new Class[] { String.class })); String reverseString=parser.parseExpression(“#reverse(”main”)”).getValue(context, String.class); System.out.println(reverseString); } public static String reverse(String input) { StringBuilder backwards = new StringBuilder(); for (int i = 0; i < input.length(); i++) { backwards.append(input.charAt(input.length() – 1 – i)); } return backwards.toString(); } } Output niam Print Page Previous Next Advertisements ”;
Spring SpEL – Overview
Spring SpEL – Overview ”; Previous Next 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. Following is the list of functionalities that Spring Expression Language, SpEL supports: Literal expressions Boolean and relational operators Regular expressions Class expressions Accessing properties, arrays, lists, maps Method invocation Relational operators Assignment Calling constructors Bean references Array construction Inline lists Ternary operator Variables User defined functions Collection projection Collection selection Templated expressions We”ll cover each and every topic in next chapters. Print Page Previous Next Advertisements ”;
Spring SpEL – Assignment Operator ”; Previous Next SpEL expression supports assignment of properties using setValue() method as well as using assignment operator. 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 object. 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); // Using setValue parser.parseExpression(“name”).setValue(employeeContext, “Mahesh”); String result = parser.parseExpression(“name”).getValue(employeeContext, String.class); System.out.println(result); // Using assignment operator result = parser.parseExpression(“Name = ”Robert””).getValue(employeeContext, String.class); System.out.println(result); } } Output Mahesh Robert Print Page Previous Next Advertisements ”;