0
在彈簧表達式(SpEL)中支持的投影集合對象的設置值?春季表現(SpEL)支持的投影集合對象的字段的設定值是?
嘗試運行示例代碼時出現以下錯誤。
Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1068E:(pos 1310742): the expression component '![comment]' is not assignable
我想要做的是 - 在所有符合特定條件的對象上設置一個值。 (因爲我想保持它的簡單和快捷下面的例子可能不符合最佳實踐)
package com.test;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.ArrayList;
import java.util.List;
public class SpELTest {
public static class Person {
public String name;
public int age;
public String comment;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", comment='" + comment + '\'' +
'}';
}
}
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Person 1", 10));
people.add(new Person("Person 2", 20));
people.add(new Person("Person 3", 30));
people.add(new Person("Person 4", 40));
people.add(new Person("Person 5", 50));
people.add(new Person("Person 6", 60));
people.add(new Person("Person 7", 70));
people.add(new Person("Person 8", 80));
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("people", people);
// check the projected
List<String> projected = (List<String>) parser.parseExpression("#people.?[age > 50].![name]").getValue(context);
for (String p : projected) {
System.out.println(p);
}
// use expression to set values
parser.parseExpression("#people.?[age > 50].![comment]").setValue(context, "older than 50");
for (Person p : people) {
System.out.println(p);
}
}
}
輸出:
Person 6
Person 7
Person 8
Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1068E:(pos 1310742): the expression component '![comment]' is not assignable
at org.springframework.expression.spel.ast.ValueRef$TypedValueHolderValueRef.setValue(ValueRef.java:78)
at org.springframework.expression.spel.ast.CompoundExpression.setValue(CompoundExpression.java:87)
at org.springframework.expression.spel.standard.SpelExpression.setValue(SpelExpression.java:169)
at com.test.SpELTest.main(SpELTest.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:88)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
at java.lang.reflect.Method.invoke(Method.java:613)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)