SpEL 表达式
我们直接使用了 Infra EL
SpEL 是一种强大的表达语言,支持在运行时查询和操作对象图。该语言的语法类似于 Jakarta Expression Language, 但提供了额外的功能,尤其是方法调用和基本的字符串模板功能。
While there are several other Java expression languages available — OGNL, MVEL, and JBoss EL, to name a few — the Infra Expression Language was created to provide the Infra community with a single well supported expression language that can be used across all the products in the Infra portfolio. Its language features are driven by the requirements of the projects in the Infra portfolio, including tooling requirements for code completion support within the Infra Tools for Eclipse. That said, SpEL is based on a technology-agnostic API that lets other expression language implementations be integrated, should the need arise.
While SpEL serves as the foundation for expression evaluation within the Infra portfolio, it is not directly tied to Infra and can be used independently. To be self contained, many of the examples in this chapter use SpEL as if it were an independent expression language. This requires creating a few bootstrapping infrastructure classes, such as the parser. Most Infra users need not deal with this infrastructure and can, instead, author only expression strings for evaluation. An example of this typical use is the integration of SpEL into creating XML or annotation-based bean definitions, as shown in Expression support for defining bean definitions.
This chapter covers the features of the expression language, its API, and its language
syntax. In several places, Inventor
and Society
classes are used as the target
objects for expression evaluation. These class declarations and the data used to
populate them are listed at the end of the chapter.
The expression language supports the following functionality:
-
Literal expressions
-
Accessing properties, arrays, lists, and maps
-
Inline lists
-
Inline maps
-
Array construction
-
Relational operators
-
Regular expressions
-
Logical operators
-
String operators
-
Mathematical operators
-
Assignment
-
Type expressions
-
Method invocation
-
Constructor invocation
-
Variables
-
User-defined functions
-
Bean references
-
Ternary, Elvis, and safe-navigation operators
-
Collection projection
-
Collection selection
-
Templated expressions