变量

您可以使用#variableName语法。变量 使用setVariable()method 中EvaluationContext实现。spring-doc.cadn.net.cn

变量名称必须以字母(定义见下文)、下划线或美元开头 标志。spring-doc.cadn.net.cn

变量名称必须由以下一种或多种受支持的 字符。spring-doc.cadn.net.cn

EvaluationContext,建议 变量或根上下文对象的类型为public.spring-doc.cadn.net.cn

否则,涉及变量或根上下文对象的某些类型的 SPEL 表达式 使用非公共类型时,可能无法计算或编译。spring-doc.cadn.net.cn

由于变量与评估上下文中的函数共享一个公共命名空间,因此 必须注意确保变量名称和函数名称不重叠。spring-doc.cadn.net.cn

以下示例演示如何使用变量。spring-doc.cadn.net.cn

Inventor tesla = new Inventor("Nikola Tesla", "Serbian");

EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
context.setVariable("newName", "Mike Tesla");

parser.parseExpression("name = #newName").getValue(context, tesla);
System.out.println(tesla.getName());  // "Mike Tesla"
val tesla = Inventor("Nikola Tesla", "Serbian")

val context = SimpleEvaluationContext.forReadWriteDataBinding().build()
context.setVariable("newName", "Mike Tesla")

parser.parseExpression("name = #newName").getValue(context, tesla)
println(tesla.name)  // "Mike Tesla"

#this#root变量

#thisvariable 始终被定义并引用当前评估对象 (根据这些引用解析不合格的引用)。这#rootvariable 始终 定义并引用根上下文对象。虽然#this可能因 计算表达式,#root始终引用根。spring-doc.cadn.net.cn

以下示例演示如何使用#this变量与集合选择结合使用。spring-doc.cadn.net.cn

// Create a list of prime integers.
List<Integer> primes = List.of(2, 3, 5, 7, 11, 13, 17);

// Create parser and set variable 'primes' as the list of integers.
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
context.setVariable("primes", primes);

// Select all prime numbers > 10 from the list (using selection ?{...}).
String expression = "#primes.?[#this > 10]";

// Evaluates to a list containing [11, 13, 17].
List<Integer> primesGreaterThanTen =
		parser.parseExpression(expression).getValue(context, List.class);
// Create a list of prime integers.
val primes = listOf(2, 3, 5, 7, 11, 13, 17)

// Create parser and set variable 'primes' as the list of integers.
val parser = SpelExpressionParser()
val context = SimpleEvaluationContext.forReadWriteDataBinding().build()
context.setVariable("primes", primes)

// Select all prime numbers > 10 from the list (using selection ?{...}).
val expression = "#primes.?[#this > 10]"

// Evaluates to a list containing [11, 13, 17].
val primesGreaterThanTen = parser.parseExpression(expression)
		.getValue(context) as List<Int>

以下示例演示如何使用#this#root变量一起放入 与集合投影相结合。spring-doc.cadn.net.cn

// Create parser and evaluation context.
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

// Create an inventor to use as the root context object.
Inventor tesla = new Inventor("Nikola Tesla");
tesla.setInventions("Telephone repeater", "Tesla coil transformer");

// Iterate over all inventions of the Inventor referenced as the #root
// object, and generate a list of strings whose contents take the form
// "<inventor's name> invented the <invention>." (using projection !{...}).
String expression = "#root.inventions.![#root.name + ' invented the ' + #this + '.']";

// Evaluates to a list containing:
// "Nikola Tesla invented the Telephone repeater."
// "Nikola Tesla invented the Tesla coil transformer."
List<String> results = parser.parseExpression(expression)
		.getValue(context, tesla, List.class);
// Create parser and evaluation context.
val parser = SpelExpressionParser()
val context = SimpleEvaluationContext.forReadWriteDataBinding().build()

// Create an inventor to use as the root context object.
val tesla = Inventor("Nikola Tesla")
tesla.setInventions("Telephone repeater", "Tesla coil transformer")

// Iterate over all inventions of the Inventor referenced as the #root
// object, and generate a list of strings whose contents take the form
// "<inventor's name> invented the <invention>." (using projection !{...}).
val expression = "#root.inventions.![#root.name + ' invented the ' + #this + '.']"

// Evaluates to a list containing:
// "Nikola Tesla invented the Telephone repeater."
// "Nikola Tesla invented the Tesla coil transformer."
val results = parser.parseExpression(expression)
		.getValue(context, tesla, List::class.java)