我正在尝试在 Spring @Cache 注释中使用 Java 8 流和 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, 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 表达式中使用流。这可以吗?
最佳答案
不要让反对者妨碍你,即使他们作者接受了答案! :) 只要发挥一点创意,你就可以实现你的意图!以下语法(使用 Collection Selection 和 'this')
此处#root 是您的实体,在选择范围内,#this 指的是标签。
任意匹配示例:
"#root.getTags().?[#this.getId() == null].size() > 0"
示例 allMatch:
"#root.getTags().?[#this.getId() == null].size() eq #root.getTags().size()"
关于spring - 为什么这个流和 lambda 表达式不能与 SpELL 声明一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32132214/