Spring 为什么';t这条小溪&;lambda表达式是否与SpEL声明一起工作?
我试图在Spring@Cache注释中使用Java8流和lambda表达式 我尝试使用以下方法:Spring 为什么';t这条小溪&;lambda表达式是否与SpEL声明一起工作?,spring,spring-el,spring-cache,Spring,Spring El,Spring Cache,我试图在Spring@Cache注释中使用Java8流和lambda表达式 我尝试使用以下方法: @CacheEvict(value = "tags", allEntries = true, condition = "#entity.getTags().stream().anyMatch(tag -> tag.getId() == null)") 它的失败在于: SEVERE: The RuntimeException could not be mapped to a response
@CacheEvict(value = "tags", allEntries = true,
condition = "#entity.getTags().stream().anyMatch(tag -> tag.getId() == null)")
它的失败在于:
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.springframework.expression.spel.SpelParseException:
EL1042E:(pos 40): Problem parsing right operand
但是,如果我将流移动到实体上的方法中,我就能够让它工作。然后,注释按如下方式工作,不会出现错误:
@CacheEvict(value = "tags", beforeInvocation=true, allEntries = true,
condition = "#entity.containsNewTag()")
我不希望使用“containtsNewTag()”方法,如果可能的话,可以直接在SpEL表达式中使用流。可以这样做吗?定义了Spring表达式语言。您正在尝试的操作目前不受该语言的支持。我还认为这是一个放置此类代码的非常奇怪的地方:一个可以进行单元测试的独立方法确实要好得多。您可以使用以下语法(使用和“this”)来实现您的意图 这里#root是您的实体,在选择中#这指的是一个标记 示例anyMatch:
"#root.getTags().?[#this.getId() == null].size() > 0"
示例allMatch:
"#root.getTags().?[#this.getId() == null].size() eq #root.getTags().size()"