【问题标题】:Why doesn't this stream & lambda expression work with SpEL declaration?为什么这个流和 lambda 表达式不能与 SpELL 声明一起使用?
【发布时间】:2015-11-14 22:04:59
【问题描述】:

我正在尝试在 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 表达式中使用流。这个可以吗?

【问题讨论】:

    标签: spring spring-el spring-cache


    【解决方案1】:

    Spring 表达式语言定义为in the developer guide。目前该语言不支持您尝试执行的操作。我还认为这是放置此类代码的一个非常奇怪的地方:您可以进行单元测试的隔离方法确实要好得多。

    【讨论】:

      【解决方案2】:

      您可以使用以下语法来实现您的意图(使用Collection Selection 和 'this')

      这里#root 是你的实体,而在选择中,#this 指的是一个标签。

      anyMatch 示例:

      "#root.getTags().?[#this.getId() == null].size() > 0"
      

      allMatch 示例:

      "#root.getTags().?[#this.getId() == null].size() eq #root.getTags().size()"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-27
        • 2015-01-23
        • 1970-01-01
        • 2019-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多