Spring Expression Language - List上的Size属性

时间:2014-11-19 11:24:40

标签: java spring expression

我正在尝试使用Spring EL在List上使用size属性,弹出EL会抛出无法找到大小的异常。

@Cacheable(value = "cache", unless = "#result.size > 0")
public List<Results> getResult();

例外:

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 8): Field or property 'size' cannot be found on object of type 'java.util.ArrayList'
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:85)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:43)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:346)
        at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:84)

2 个答案:

答案 0 :(得分:5)

size解析为List#getSize()并不存在。请尝试使用size(),如下所示:

@Cacheable(value = "cache", unless = "#result.size() > 0")
public List<Results> getResult();

答案 1 :(得分:3)

正如错误所述,size不是ArrayList的属性,它是一个函数。试试size()