- Spring SpEL - Discussion
- Spring SpEL - Useful Resources
- Spring SpEL - Quick Guide
- Spring SpEL - Expression Templating
- Spring SpEL - Functions
- Spring SpEL - Variables
- Spring SpEL - Constructor
- Spring SpEL - Collection Projection
- Spring SpEL - Collection Selection
- Spring SpEL - Safe Navigation Operator
- Spring SpEL - Elvis Operator
- Spring SpEL - Ternary Operator
- Spring SpEL - Assignment Operator
- Spring SpEL - Mathematical Operators
- Spring SpEL - Logical Operators
- Spring SpEL - Relational Operators
- Spring SpEL - Methods
- Spring SpEL - Map
- Spring SpEL - List
- Spring SpEL - Array
- Spring SpEL - Properties
- Spring SpEL - Literal Expression
- Spring SpEL - Annotation Configuration
- Spring SpEL - XML Configuration
- Spring SpEL - EvaluationContext
- Spring SpEL - Expression Interface
- Spring SpEL - Create Project
- Spring SpEL - Environment Setup
- Spring SpEL - Overview
- Spring SpEL - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring SpEL - Expression Interface
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 calpng methods, calpng 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
chapter. We re adding following files −MainApp.java − Main apppcation 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; pubpc class MainApp { pubpc 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); //Calpng 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 TUTORIALSPOINTAdvertisements