2016-03-08 71 views
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) 

回答

0

有在代碼中的兩個問題。

首先 - 你不能對一些簡單的屬性進行投影。

讓我們考慮下面的類:

class Person { 
    String name 
} 

這將有可能通過

person.name = "newValue" 

但是你想做什麼改變Person的名字是:

String projection = person.name 
projection = "newValue" 

這不起作用。

第二件事是您不能將值older than 50賦值給一個集合(List)。 你能夠做這樣的事:

parser.parseExpression("(#people.?[age > 50 and comment == null])[0].comment") 
     .setValue(context, "older than 50"); 

,但這將改變只有在導致投影中的第一項。

SpEL沒有簡單的方法可以做到這一點,但您可以通過context.registerFunction註冊它們來使用一些外部功能。這種功能可以接受Person對象的集合並對它們執行突變。