|
此版本仍在开发中,尚未被视为稳定版。如需最新稳定版本,请使用 Spring Framework 7.0.6! |
属性、数组、列表、映射和索引器
Spring 表达式语言(SpEL)提供了对导航对象图以及对各种结构进行索引的支持。
| 数值索引值从零开始,例如在 Java 中访问数组的第 nth 个元素时。 |
| 有关如何使用空安全操作符(null-safe operator)遍历对象图以及对各种结构进行索引的详细信息,请参阅安全导航操作符部分。 |
属性导航
你可以使用句点(.)来表示嵌套的属性值,从而在对象图中导航属性引用。Inventor 类的实例 pupin 和 tesla 已使用
示例中使用的类 部分列出的数据进行了填充。为了
向下遍历对象图并获取特斯拉的出生年份和普平的出生城市,我们使用以下表达式:
-
Java
-
Kotlin
// evaluates to 1856
int year = (Integer) parser.parseExpression("birthdate.year + 1900").getValue(context);
// evaluates to "Smiljan"
String city = (String) parser.parseExpression("placeOfBirth.city").getValue(context);
// evaluates to 1856
val year = parser.parseExpression("birthdate.year + 1900").getValue(context) as Int
// evaluates to "Smiljan"
val city = parser.parseExpression("placeOfBirth.city").getValue(context) as String
|
属性名称的首字母允许不区分大小写。因此,上面示例中的表达式可以分别写作 |
数组和集合的索引访问
可以通过使用方括号表示法来获取数组或集合(例如 Set 或 List)的第 nth 个元素,如下例所示。
|
如果被索引的集合是 对于任何其他类型的 |
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
// Inventions Array
// evaluates to "Induction motor"
String invention = parser.parseExpression("inventions[3]").getValue(
context, tesla, String.class);
// Members List
// evaluates to "Nikola Tesla"
String name = parser.parseExpression("members[0].name").getValue(
context, ieee, String.class);
// List and Array Indexing
// evaluates to "Wireless communication"
String invention = parser.parseExpression("members[0].inventions[6]").getValue(
context, ieee, String.class);
val parser = SpelExpressionParser()
val context = SimpleEvaluationContext.forReadOnlyDataBinding().build()
// Inventions Array
// evaluates to "Induction motor"
val invention = parser.parseExpression("inventions[3]").getValue(
context, tesla, String::class.java)
// Members List
// evaluates to "Nikola Tesla"
val name = parser.parseExpression("members[0].name").getValue(
context, ieee, String::class.java)
// List and Array Indexing
// evaluates to "Wireless communication"
val invention = parser.parseExpression("members[0].inventions[6]").getValue(
context, ieee, String::class.java)
字符串索引
字符串的第 nth 个字符可以通过在方括号内指定索引来获取,如下例所示。
字符串的第 nth 个字符将被解析为 java.lang.String,而不是 java.lang.Character。 |
-
Java
-
Kotlin
// evaluates to "T" (8th letter of "Nikola Tesla")
String character = parser.parseExpression("members[0].name[7]")
.getValue(societyContext, String.class);
// evaluates to "T" (8th letter of "Nikola Tesla")
val character = parser.parseExpression("members[0].name[7]")
.getValue(societyContext, String::class.java)
映射的索引访问
通过在方括号内指定键值来获取映射(map)的内容。在以下示例中,由于 officers 映射的键是字符串,我们可以指定诸如 'president' 这样的字符串字面量:
-
Java
-
Kotlin
// Officer's Map
// evaluates to Inventor("Pupin")
Inventor pupin = parser.parseExpression("officers['president']")
.getValue(societyContext, Inventor.class);
// evaluates to "Idvor"
String city = parser.parseExpression("officers['president'].placeOfBirth.city")
.getValue(societyContext, String.class);
String countryExpression = "officers['advisors'][0].placeOfBirth.country";
// setting values
parser.parseExpression(countryExpression)
.setValue(societyContext, "Croatia");
// evaluates to "Croatia"
String country = parser.parseExpression(countryExpression)
.getValue(societyContext, String.class);
// Officer's Map
// evaluates to Inventor("Pupin")
val pupin = parser.parseExpression("officers['president']")
.getValue(societyContext, Inventor::class.java)
// evaluates to "Idvor"
val city = parser.parseExpression("officers['president'].placeOfBirth.city")
.getValue(societyContext, String::class.java)
val countryExpression = "officers['advisors'][0].placeOfBirth.country"
// setting values
parser.parseExpression(countryExpression)
.setValue(societyContext, "Croatia")
// evaluates to "Croatia"
val country = parser.parseExpression(countryExpression)
.getValue(societyContext, String::class.java)
对象索引
可以通过在方括号内指定属性名称来获取对象的属性。这类似于根据键(key)访问映射(map)中的值。以下示例演示了如何通过索引访问对象以检索特定属性。
-
Java
-
Kotlin
// Create an inventor to use as the root context object.
Inventor tesla = new Inventor("Nikola Tesla");
// evaluates to "Nikola Tesla"
String name = parser.parseExpression("#root['name']")
.getValue(context, tesla, String.class);
// Create an inventor to use as the root context object.
val tesla = Inventor("Nikola Tesla")
// evaluates to "Nikola Tesla"
val name = parser.parseExpression("#root['name']")
.getValue(context, tesla, String::class.java)
索引到自定义结构中
自 Spring Framework 6.2 起,Spring 表达式语言支持通过索引访问自定义数据结构,方法是允许开发者实现并注册一个 IndexAccessor 到 EvaluationContext 中。如果您希望支持对依赖自定义索引访问器的表达式进行编译,那么该索引访问器必须实现 CompilableIndexAccessor SPI。
为了支持常见用例,Spring 提供了一个内置的 ReflectiveIndexAccessor,
它是一个灵活的 IndexAccessor,使用反射从目标对象的索引结构中读取数据,
并可选择性地向其中写入数据。该索引结构可通过一个 public 的读取方法(读取时)
或一个 public 的写入方法(写入时)进行访问。
读取方法与写入方法之间的关系基于一种适用于典型索引结构实现的约定。
ReflectiveIndexAccessor 还实现了 CompilableIndexAccessor,以支持通过 编译 生成字节码进行读取访问。但请注意,配置的 read-method 必须能够通过 public 类或 public 接口调用,编译才能成功。 |
以下代码清单定义了一个 Color 枚举和一个 FruitMap 类型,该类型的行为类似于映射(map),但并未实现 java.util.Map 接口。因此,如果你想在 SpEL 表达式中对 FruitMap 进行索引访问,则需要注册一个 IndexAccessor。
public enum Color {
RED, ORANGE, YELLOW
}
public class FruitMap {
private final Map<Color, String> map = new HashMap<>();
public FruitMap() {
this.map.put(Color.RED, "cherry");
this.map.put(Color.ORANGE, "orange");
this.map.put(Color.YELLOW, "banana");
}
public String getFruit(Color color) {
return this.map.get(color);
}
public void setFruit(Color color, String fruit) {
this.map.put(color, fruit);
}
}
可以通过 IndexAccessor 创建一个用于 FruitMap 的只读 new
ReflectiveIndexAccessor(FruitMap.class, Color.class, "getFruit")。在注册了该访问器并将一个 FruitMap 实例注册为名为 #fruitMap 的变量后,SpEL 表达式 #fruitMap[T(example.Color).RED] 将求值为 "cherry"。
可以通过 IndexAccessor 创建一个用于 FruitMap 的读写型 new
ReflectiveIndexAccessor(FruitMap.class, Color.class, "getFruit", "setFruit")。注册该访问器并将一个 FruitMap 实例注册为名为 #fruitMap 的变量后,即可使用 SpEL 表达式 #fruitMap[T(example.Color).RED] = 'strawberry' 将红色对应的水果映射从 "cherry" 修改为 "strawberry"。
以下示例演示了如何注册一个 ReflectiveIndexAccessor 以对 FruitMap 进行索引,然后在 SpEL 表达式中对 FruitMap 进行索引。
-
Java
-
Kotlin
// Create a ReflectiveIndexAccessor for FruitMap
IndexAccessor fruitMapAccessor = new ReflectiveIndexAccessor(
FruitMap.class, Color.class, "getFruit", "setFruit");
// Register the IndexAccessor for FruitMap
context.addIndexAccessor(fruitMapAccessor);
// Register the fruitMap variable
context.setVariable("fruitMap", new FruitMap());
// evaluates to "cherry"
String fruit = parser.parseExpression("#fruitMap[T(example.Color).RED]")
.getValue(context, String.class);
// Create a ReflectiveIndexAccessor for FruitMap
val fruitMapAccessor = ReflectiveIndexAccessor(
FruitMap::class.java, Color::class.java, "getFruit", "setFruit")
// Register the IndexAccessor for FruitMap
context.addIndexAccessor(fruitMapAccessor)
// Register the fruitMap variable
context.setVariable("fruitMap", FruitMap())
// evaluates to "cherry"
val fruit = parser.parseExpression("#fruitMap[T(example.Color).RED]")
.getValue(context, String::class.java)