【问题标题】:Spring Cacheable how to pass method invocation result to key?Spring Cacheable如何将方法调用结果传递给键?
【发布时间】:2017-01-05 19:55:56
【问题描述】:

考虑以下 sn-p,我正在尝试调用 propertiesContainer 的方法,该方法将用作键。

@Cacheable(value = EhCacheManagerApi.CACHE_X_TOKEN, key = ("#{propertiesContainer.getId()}"))
public String getToken(PropertiesContainer propertiesContainer)

我似乎无法找出键的正确拼写表达式,目前的格式给了我:

org.springframework.expression.spel.SpelParseException: EL1043E:(pos 1): Unexpected token. Expected 'identifier' but was 'lcurly({)'

在我尝试key = ("#propertiesContainer.id")key = ("#propertiesContainer.getId()")之前

propertiesContainer 是一个接口,其方法getId 返回String

所以大概这与使用 SpEL 的 bean 方法调用不一样?

【问题讨论】:

    标签: spring spring-el


    【解决方案1】:

    你可以试试这个

        @Cacheable(value = EhCacheManagerApi.CACHE_X_TOKEN, key = "#{T(java.lang.String).format('%d-%d', #propertiesContainer.id)}")
    

    【讨论】:

      【解决方案2】:

      你试过不带括号的纯表达式吗:

      @Cacheable(value = EhCacheManagerApi.CACHE_X_TOKEN, key="propertiesContainer.id")
      

      这在 Spring 4.3.3 中适用于我,其中 PropertiesContainer 是一个带有 getId() 方法的接口。

      另外,如果您的编译代码中没有调试信息,您可能需要使用#p0.id 而不是方法参数名称。请参阅the accepted answer here,但我怀疑这会给您带来不同的错误。

      【讨论】:

        猜你喜欢
        • 2016-02-21
        • 2017-02-15
        • 2021-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-08
        • 1970-01-01
        相关资源
        最近更新 更多