Spring Expression Language (SpEL)

Spring 表达式语言(简称“SpEL”)是一种功能强大的表达式语言,它支持在运行时查询和操作对象图。该语言的语法与 Jakarta Expression Language 类似,但提供了其他功能,最主要是方法调用和基本字符串模板化功能。

The Spring Expression Language ("SpEL" for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to the Jakarta Expression Language but offers additional features, most notably method invocation and basic string templating functionality.

虽然还有其他一些 Java 表达式语言可用——比如 OGNL、MVEL 和 JBossEL——Spring 表达式语言的创建是为了向 Spring 社区提供一个受良好支持的表达式语言,该语言可以在 Spring 产品组合中的所有产品中使用。其语言特性由 Spring 产品组合中项目的需要所驱动,包括 https://spring.io/tools [Spring Tools for Eclipse] 中对代码补全支持的工具需求。也就是说,SpEL 基于与技术无关的 API,该 API 允许集成其他表达式语言实现(如有需要)。

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. Its language features are driven by the requirements of the projects in the Spring portfolio, including tooling requirements for code completion support within the Spring 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.

尽管 SpEL 作为 Spring 产品组合中表达式计算的基础,但它与 Spring 没有直接联系,而且可以独立使用。为了自包含,本章中的许多示例都将 SpEL 用作独立的表达式语言。这需要创建一些引导基础架构类,例如解析器。大多数 Spring 用户不必处理此基础架构,而只需编写表达式字符串以进行计算。一个典型用途的示例是将 SpEL 集成到创建基于 XML 或基于注释的 Bean 定义中,如 Expression support for defining bean definitions 中所示。

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. 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 Spring 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.

此章节涵盖了表达式语言的功能、其 API 及其语言语法。在多个地方,InventorSociety 类被用作表达求值的目标对象。这些类的声明和用来填充这些类的相关数据在章节末尾列出。

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