Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some valid uses for spEL (Spring Expression Language) ?

Tags:

java

spring

I was looking at spring expression language and it seems very useful if you are using for configuration xml files. However, the reference docs provide lot of examples that use the following code below:

ExpressionParser parser = new SpelExpressionParser();

String name = parser.parseExpression

When and why would I be using this as I can do all of it using regular java. Am I missing some features or other uses where SpelExpressionParser could be handy? Are there other uses cases for spEL that I'm not aware of?

like image 733
user373201 Avatar asked Dec 25 '11 16:12

user373201


People also ask

Is expression language a part of core Spring Framework?

While there are several other Java expression languages available, OGNL, MVEL, and JBoss EL, to name a few, the Spring Expression Language was created to provide the Spring community with a single well supported expression language that can be used across all the products in the Spring portfolio.

What is a expression language?

An expression language is a language for creating a computer-interpretable representation of specific knowledge and may refer to: Advanced Boolean Expression Language, an obsolete hardware description language for hardware descriptions.

What is expression language in JSP?

JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical.

What is StandardEvaluationContext?

public class StandardEvaluationContext extends Object implements EvaluationContext. A powerful and highly configurable EvaluationContext implementation. This context uses standard implementations of all applicable strategies, based on reflection to resolve properties, methods and fields.


2 Answers

You can use SpEL as a simplified alternative to Java reflection. It reduces a lot of code for you.
One of the use cases, I had at my work is to flatten a deep object graph to a name-value pair for indexing and lookup purposes.

The object graph can be extended by our customers in which case the additional properties should also be indexed. By using SpEL, the customers only need to write expressions in some properties file and the framework code takes care of extracting the new data.

All the use-cases of Java reflection are also potential use-cases for SpEL with added simplicity and reduced code.

like image 67
Aravind Yarram Avatar answered Sep 22 '22 19:09

Aravind Yarram


In order to evaluate arbitrary runtime expressions in java you'd need to use the compilation framework, create a class, compile it, load it, bind it, execute it, ...

Expression languages and/or scripting eliminate a lot of additional work not directly related to the problem you're trying to solve. Which EL makes the most sense to use depends on several factors.

like image 45
Dave Newton Avatar answered Sep 21 '22 19:09

Dave Newton