spring - 为什么这个流和 lambda 表达式不能与 SpELL 声明一起使用?

标签 spring spring-el spring-cache

我正在尝试在 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/

相关文章:

java - Spring mvc看不到jsp

java - Spring Boot缓存中的咖啡因缓存: Get all cached keys

spring - 为什么 Spring @Cacheable 不将带注释的方法的结果类型传递给它的反序列化器?

spring - eclipse 将 web.xml 错误地加载到 tomcat

java - 带有 spring boot 的 tiles url 映射

java - 在 Spring Data 中更新

spring - 在 Spring Boot 中的 application.properties 中获取用户主路径

spring - 是否可以在 ${...} 表达式中传递 NULL 作为参数?

spring - SpEL - 空值比较

java - timetoliveseconds ehcache spring boot 配置不起作用