變數

您可以使用 #variableName 語法在表示式中引用變數。變數透過 EvaluationContext 實現中的 setVariable() 方法設定。

變數名稱必須以字母(如下定義)、下劃線或美元符號開頭。

變數名稱必須由以下一種或多種受支援的字元型別組成。

  • 字母:任何 java.lang.Character.isLetter(char) 返回 true 的字元

    • 這包括 AZazüñé 等字母,以及中文、日文、西里爾文等其他字元集中的字母。

  • 數字:09

  • 下劃線:_

  • 美元符號:$

EvaluationContext 中設定變數或根上下文物件時,建議將變數或根上下文物件的型別設定為 public

否則,涉及非公共型別變數或根上下文物件的某些型別的 SpEL 表示式可能無法求值或編譯。

由於變數與求值上下文中的 函式 共享一個共同的名稱空間,因此必須注意確保變數名和函式名不重疊。

以下示例展示瞭如何使用變數。

  • Java

  • Kotlin

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 變數

#this 變數始終定義並指向當前求值物件(不限定名稱引用以此物件解析)。#root 變數始終定義並指向根上下文物件。雖然表示式求值過程中 #this 可能會改變,但 #root 始終指向根物件。

以下示例展示瞭如何將 #this 變數與 集合選擇 結合使用。

  • Java

  • Kotlin

// 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 變數與 集合投影 一起使用。

  • Java

  • Kotlin

// 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)