Spring spel expression tutorial part 1

Overview of Spring SpEL expression tutorial:

In this article, We are going learn Spring SpEL expression tutorial with an example. The Spring SpEL expression is a powerful expression language that supports querying and manipulating an object graph at run-time. SpEL stands for Spring Expression Language.

The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.

In the market, there are several languages are available such as OGNL, MVEL, and JBoss EL, to name a few more.

The Spring SpEL expression 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.

Spring SpEL expression provides some additional features such as method invocation and string templating functionality.

Features:

  • 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
  • Inline maps
  • Ternary operator
  • Variables
  • User-defined functions
  • Collection Projection
  • Collection selection
  • Templated expressions

Where to use SpEL?

We can use Spel at the time of dynamic bean wiring (dependency injection) at runtime. In particular, you can use SpEL in conditional situations to pick the right bean or value to dependency inject based on what is happening and discovered in the application.

Simple Example:

  • The SpEL classes and interfaces you are most likely to use are located in the packages org.springframework.expression.
  • ExpressionParser: This interface is responsible for parsing the expression string.
  • Expression: This interface is responsible for evaluating the previously defined expression string.
  • There are two exceptions that can be thrown, ParseException and EvaluationException when calling parser.parseExpression and exp.getValue respectively.
  • SpEL supports a wide range of features, such as calling methods, accessing properties, and calling constructors.
  • SpEL also supports nested properties using standard dot notation, i.e. prop1.prop2.prop3 and the setting of property values.

  • We can use the string constructor instead of a string literal. Below is the example.

Next article we are going to learn more about Spring SpEL expression tutorial with an example.

Happy learning…

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons