|
对于最新的稳定版本,请使用 Spring Framework 6.2.7! |
安全导航作员
安全导航运算符 (?) 用于避免NullPointerException然后来了
来自 Groovy 语言。通常,当您引用某个对象时,可能需要验证
它不是null在访问对象的方法或属性之前。为避免
this 时,安全导航运算符会返回null对于特定的 null 安全作
而不是引发异常。
|
当安全导航作员评估为 有关详细信息,请参阅复合表达式中的 Null 安全作。 |
安全属性和方法访问
以下示例演示如何使用安全导航运算符进行属性访问
(?.).
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));
// evaluates to "Smiljan"
String city = parser.parseExpression("placeOfBirth?.city") (1)
.getValue(context, tesla, String.class);
tesla.setPlaceOfBirth(null);
// evaluates to null - does not throw NullPointerException
city = parser.parseExpression("placeOfBirth?.city") (2)
.getValue(context, tesla, String.class);
| 1 | 对非 null 使用安全导航运算符placeOfBirth属性 |
| 2 | 对 null 使用安全导航运算符placeOfBirth属性 |
val parser = SpelExpressionParser()
val context = SimpleEvaluationContext.forReadOnlyDataBinding().build()
val tesla = Inventor("Nikola Tesla", "Serbian")
tesla.setPlaceOfBirth(PlaceOfBirth("Smiljan"))
// evaluates to "Smiljan"
var city = parser.parseExpression("placeOfBirth?.city") (1)
.getValue(context, tesla, String::class.java)
tesla.setPlaceOfBirth(null)
// evaluates to null - does not throw NullPointerException
city = parser.parseExpression("placeOfBirth?.city") (2)
.getValue(context, tesla, String::class.java)
| 1 | 对非 null 使用安全导航运算符placeOfBirth属性 |
| 2 | 对 null 使用安全导航运算符placeOfBirth属性 |
|
安全导航运算符也适用于对对象的方法调用。 例如,表达式 |
安全收藏夹选择和投影
Spring 表达式语言支持通过 以下运算符。
-
空安全选择:
?.? -
空安全 select first:
?.^ -
空安全 select last:
?.$ -
空安全投影:
?.!
以下示例演示如何使用安全导航运算符进行收集
选择 (?.?).
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
IEEE society = new IEEE();
StandardEvaluationContext context = new StandardEvaluationContext(society);
String expression = "members?.?[nationality == 'Serbian']"; (1)
// evaluates to [Inventor("Nikola Tesla")]
List<Inventor> list = (List<Inventor>) parser.parseExpression(expression)
.getValue(context);
society.members = null;
// evaluates to null - does not throw a NullPointerException
list = (List<Inventor>) parser.parseExpression(expression)
.getValue(context);
| 1 | 对可能为 null 的 Factor 使用 null 安全选择运算符Use null-safe selection operator on potentially nullmembers列表 |
val parser = SpelExpressionParser()
val society = IEEE()
val context = StandardEvaluationContext(society)
val expression = "members?.?[nationality == 'Serbian']" (1)
// evaluates to [Inventor("Nikola Tesla")]
var list = parser.parseExpression(expression)
.getValue(context) as List<Inventor>
society.members = null
// evaluates to null - does not throw a NullPointerException
list = parser.parseExpression(expression)
.getValue(context) as List<Inventor>
| 1 | 对可能为 null 的 Factor 使用 null 安全选择运算符Use null-safe selection operator on potentially nullmembers列表 |
以下示例显示了如何使用 “null-safe select first” 运算符
集合 (?.^).
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
IEEE society = new IEEE();
StandardEvaluationContext context = new StandardEvaluationContext(society);
String expression =
"members?.^[nationality == 'Serbian' || nationality == 'Idvor']"; (1)
// evaluates to Inventor("Nikola Tesla")
Inventor inventor = parser.parseExpression(expression)
.getValue(context, Inventor.class);
society.members = null;
// evaluates to null - does not throw a NullPointerException
inventor = parser.parseExpression(expression)
.getValue(context, Inventor.class);
| 1 | 对可能为 null 时使用 “null-safe select first” 运算符members列表 |
val parser = SpelExpressionParser()
val society = IEEE()
val context = StandardEvaluationContext(society)
val expression =
"members?.^[nationality == 'Serbian' || nationality == 'Idvor']" (1)
// evaluates to Inventor("Nikola Tesla")
var inventor = parser.parseExpression(expression)
.getValue(context, Inventor::class.java)
society.members = null
// evaluates to null - does not throw a NullPointerException
inventor = parser.parseExpression(expression)
.getValue(context, Inventor::class.java)
| 1 | 对可能为 null 时使用 “null-safe select first” 运算符members列表 |
以下示例显示了如何对
集合 (?.$).
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
IEEE society = new IEEE();
StandardEvaluationContext context = new StandardEvaluationContext(society);
String expression =
"members?.$[nationality == 'Serbian' || nationality == 'Idvor']"; (1)
// evaluates to Inventor("Pupin")
Inventor inventor = parser.parseExpression(expression)
.getValue(context, Inventor.class);
society.members = null;
// evaluates to null - does not throw a NullPointerException
inventor = parser.parseExpression(expression)
.getValue(context, Inventor.class);
| 1 | 对可能为 null 的 list last(最后选择空)运算符members列表 |
val parser = SpelExpressionParser()
val society = IEEE()
val context = StandardEvaluationContext(society)
val expression =
"members?.$[nationality == 'Serbian' || nationality == 'Idvor']" (1)
// evaluates to Inventor("Pupin")
var inventor = parser.parseExpression(expression)
.getValue(context, Inventor::class.java)
society.members = null
// evaluates to null - does not throw a NullPointerException
inventor = parser.parseExpression(expression)
.getValue(context, Inventor::class.java)
| 1 | 对可能为 null 的 list last(最后选择空)运算符members列表 |
以下示例演示如何使用安全导航运算符进行收集
投影 (?.!).
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
IEEE society = new IEEE();
StandardEvaluationContext context = new StandardEvaluationContext(society);
// evaluates to ["Smiljan", "Idvor"]
List placesOfBirth = parser.parseExpression("members?.![placeOfBirth.city]") (1)
.getValue(context, List.class);
society.members = null;
// evaluates to null - does not throw a NullPointerException
placesOfBirth = parser.parseExpression("members?.![placeOfBirth.city]") (2)
.getValue(context, List.class);
| 1 | 对非 null 使用 null 安全投影运算符members列表 |
| 2 | 对 null 使用空安全投影运算符members列表 |
val parser = SpelExpressionParser()
val society = IEEE()
val context = StandardEvaluationContext(society)
// evaluates to ["Smiljan", "Idvor"]
var placesOfBirth = parser.parseExpression("members?.![placeOfBirth.city]") (1)
.getValue(context, List::class.java)
society.members = null
// evaluates to null - does not throw a NullPointerException
placesOfBirth = parser.parseExpression("members?.![placeOfBirth.city]") (2)
.getValue(context, List::class.java)
| 1 | 对非 null 使用 null 安全投影运算符members列表 |
| 2 | 对 null 使用空安全投影运算符members列表 |
复合表达式中的 Null 安全作
如本节开头所述,当 safe navigation 运算符
计算结果为null对于复合表达式中的特定 null 安全作,
化合物表达的其余部分仍将被评估。这意味着
安全导航运算符必须应用于整个复合表达式中,以便
避免任何不必要的NullPointerException.
给定表达式#person?.address.city如果#person是null安全导航
运算符 (?.) 确保在尝试访问address的属性#person.然而,由于#person?.address计算结果为null一个NullPointerException将在尝试访问city的属性null.若要解决此问题,可以在整个复合中应用 null 安全导航
表达式,如#person?.address?.city.该表达式将安全地计算为null如果#person或#person?.address计算结果为null.
以下示例演示如何使用 “null-safe select first” 运算符
(?.^) 与 null 安全属性访问 (?.)
表达。如果members是null,则 “null-safe select first” 运算符的结果
(members?.^[nationality == 'Serbian']) 的计算结果为null以及
安全导航运算符 (?.name) 确保整个化合物表达
计算结果为null而不是引发异常。
-
Java
-
Kotlin
ExpressionParser parser = new SpelExpressionParser();
IEEE society = new IEEE();
StandardEvaluationContext context = new StandardEvaluationContext(society);
String expression = "members?.^[nationality == 'Serbian']?.name"; (1)
// evaluates to "Nikola Tesla"
String name = parser.parseExpression(expression)
.getValue(context, String.class);
society.members = null;
// evaluates to null - does not throw a NullPointerException
name = parser.parseExpression(expression)
.getValue(context, String.class);
| 1 | 在复合表达式中使用 “null-safe select first” 和 null-safe 属性访问运算符。 |
val parser = SpelExpressionParser()
val society = IEEE()
val context = StandardEvaluationContext(society)
val expression = "members?.^[nationality == 'Serbian']?.name" (1)
// evaluates to "Nikola Tesla"
String name = parser.parseExpression(expression)
.getValue(context, String::class.java)
society.members = null
// evaluates to null - does not throw a NullPointerException
name = parser.parseExpression(expression)
.getValue(context, String::class.java)
| 1 | 在复合表达式中使用 “null-safe select first” 和 null-safe 属性访问运算符。 |