【问题标题】:Spring Cacheable concat patameters for key键的Spring Cacheable concat参数
【发布时间】:2016-07-05 01:17:56
【问题描述】:

我正在使用 Spring Caching 框架和 redis。以下是我使用的 Cacheable

@Cacheable(value = "oauth2token", key="#value + #type")
public OAuth2Token findOneByValueAndType(String value, String type);

我只是想创建一个我知道的密钥。这给出了错误

14:20:21,199 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/veip-web].[Resteasy]] (http-/0.0.0.0:8080-63) JBWEB000236: Servlet.service() for servlet Resteasy threw exception: org.springframework.expression.spel.SpelEvaluationException: EL1030E:(pos 0): The operator 'ADD' is not supported between objects of type 'null' and 'null'

因为当我没有将键指定为流时。

@Cacheable(value = "oauth2token")
public OAuth2Token findOneByValueAndType(String value, String type);

我可以看到生成了一个疯狂的密钥。我需要知道密钥,因为稍后我需要CachePut 并更新相同的项目。

我也尝试过关注。

@Cacheable(value = "oauth2token", key="#type.contact(#value)")
public OAuth2Token findOneByValueAndType(String value, String type);

Spring 抱怨当时#type 为空。

这里有什么问题。

【问题讨论】:

  • 错的是你通过null.

标签: spring-mvc spring-el spring-cache


【解决方案1】:

您可能没有每个参数名称的运行时信息(#value 不知道)。更安全的方法是使用 总是 工作的 #p#a 别名。 key="#p0 + #p1" 应该可以解决问题。

话虽如此,我不会那样做。如果您在另一个带注释的方法中重用该键,我会将implement KeyGenerator 作为一个 bean 并在需要处理这两个值的方法中传递该 bean 的引用。这样您就可以共享(和测试)该代码并避免重复。

【讨论】:

  • 是的,这就是问题所在。我发现早些时候,无法更新问题。顺便说一句,我能够在通常的服务类中使用参数名称。这是我遇到此问题的 mongo 存储库界面。我认为这个参数名称不可用的问题与此有关。
猜你喜欢
  • 2012-12-13
  • 2012-08-07
  • 2019-11-19
  • 1970-01-01
  • 2012-07-08
  • 2012-12-25
  • 2015-04-15
  • 2019-10-08
  • 1970-01-01
相关资源
最近更新 更多