为什么这个stream&lambda表达式不能与SpEL声明一起使用?

Jus*_*tin 6 spring spring-el spring-cache

我正在尝试在Spring @Cache注释中使用Java 8流和lambda表达式.

我正在尝试使用以下内容:

@CacheEvict(value = "tags", allEntries = true, 
condition = "#entity.getTags().stream().anyMatch(tag -> tag.getId() == null)")
Run Code Online (Sandbox Code Playgroud)

失败的是:

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
Run Code Online (Sandbox Code Playgroud)

但是,如果我将流移动到实体上的方法中,我能够使它工作.然后注释如下工作,没有错误:

@CacheEvict(value = "tags", beforeInvocation=true, allEntries = true, 
condition = "#entity.containsNewTag()")
Run Code Online (Sandbox Code Playgroud)

我宁愿不需要'containtsNewTag()'方法,如果可能的话,直接在SpEL表达式中使用流.可以这样做吗?

man*_*ni0 9

不要让反对者妨碍您,即使他们撰写了已接受的答案!:) 只要发挥一点创意,你就可以实现你的意图!以下语法(使用集合选择和“this”)

这里#root 是您的实体,在选择范围内,#this 指的是一个标签。

任意匹配示例:

"#root.getTags().?[#this.getId() == null].size() > 0"
Run Code Online (Sandbox Code Playgroud)

示例所有匹配:

"#root.getTags().?[#this.getId() == null].size() eq #root.getTags().size()"
Run Code Online (Sandbox Code Playgroud)


Ste*_*oll 5

Spring Expression Language 在开发人员指南中定义.目前语言不支持您尝试做的事情.我还认为这是一个非常奇怪的地方放置这样的代码:一个可以单元测试的孤立方法确实更好.