Literal Expressions

SpEL 支持以下类型的字面量表达式。

SpEL supports the following types of literal expressions.

String

Strings can be delimited by single quotation marks (’`) or double quotation marks ("). To include a single quotation mark within a string literal enclosed in single quotation marks, use two adjacent single quotation mark characters. Similarly, to include a double quotation mark within a string literal enclosed in double quotation marks, use two adjacent double quotation mark characters.

Number

Numbers support the use of the negative sign, exponential notation, and decimal points.

  • Integer: int or long

  • Hexadecimal: int or long

  • Real: float or double

    • By default, real numbers are parsed using Double.parseDouble().

Boolean

true or false

Null

null

由于 Spring 表达式语言的设计和实现,字面量数字总是作为正数存储在内部。

Due to the design and implementation of the Spring Expression Language, literal numbers are always stored internally as positive numbers.

例如,\-2 在内部被存储为正数 2,然后在求值表达式期间(通过计算 0 - 2 的值)对其求反。

For example, -2 is stored internally as a positive 2 which is then negated while evaluating the expression (by calculating the value of 0 - 2).

这意味着不可能表示等于该类型数字在 Java 中的最小值的负字面量数字。 例如,Java 中支持的最小 int 值是 Integer.MIN_VALUE,其值为 -2147483648。 但是,如果在 SpEL 表达式中包含 -2147483648,将抛出一个异常,提示无法将值 2147483648 解析为 int(因为它超出了 Integer.MAX_VALUE 的值 2147483647)。

This means that it is not possible to represent a negative literal number equal to the minimum value of that type of number in Java. For example, the minimum supported value for an int in Java is Integer.MIN_VALUE which has a value of -2147483648. However, if you include -2147483648 in a SpEL expression, an exception will be thrown informing you that the value 2147483648 cannot be parsed as an int (because it exceeds the value of Integer.MAX_VALUE which is 2147483647).

如果需要在 SpEL 表达式中使用特定类型数字的最小值,可以使用相应包装器的 MIN_VALUE 常量(如 Integer.MIN_VALUE、Long.MIN_VALUE 等)来引用它,也可以计算最小值。 例如,要使用最小整数值:

If you need to use the minimum value for a particular type of number within a SpEL expression, you can either reference the MIN_VALUE constant for the respective wrapper type (such as Integer.MIN_VALUE, Long.MIN_VALUE, etc.) or calculate the minimum value. For example, to use the minimum integer value:

  • T(Integer).MIN_VALUE — requires a StandardEvaluationContext

  • -2^31 — can be used with any type of EvaluationContext

以下列表显示了字面量的简单用法。 通常,它们不是像这样单独使用,而是作为更复杂表达式的一部分,例如,在逻辑比较运算符的一侧使用字面量,或者作为方法的一个参数使用。

The following listing shows simple usage of literals. Typically, they are not used in isolation like this but, rather, as part of a more complex expression — for example, using a literal on one side of a logical comparison operator or as an argument to a method.

  • Java

  • Kotlin

ExpressionParser parser = new SpelExpressionParser();

// evaluates to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();

// evaluates to "Tony's Pizza"
String pizzaParlor = (String) parser.parseExpression("'Tony''s Pizza'").getValue();

double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();

// evaluates to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();

boolean trueValue = (Boolean) parser.parseExpression("true").getValue();

Object nullValue = parser.parseExpression("null").getValue();
val parser = SpelExpressionParser()

// evaluates to "Hello World"
val helloWorld = parser.parseExpression("'Hello World'").value as String

// evaluates to "Tony's Pizza"
val pizzaParlor = parser.parseExpression("'Tony''s Pizza'").value as String

val avogadrosNumber = parser.parseExpression("6.0221415E+23").value as Double

// evaluates to 2147483647
val maxValue = parser.parseExpression("0x7FFFFFFF").value as Int

val trueValue = parser.parseExpression("true").value as Boolean

val nullValue = parser.parseExpression("null").value