use*_*483 16 spring ehcache spelevaluationexception
我希望@Cacheable在没有参数的方法上有注释.在这种情况下,我使用@Cacheable如下
@Cacheable(value="usercache", key = "mykey")
public string sayHello(){
return "test"
}
Run Code Online (Sandbox Code Playgroud)
但是,当我调用此方法时,它不会被执行,并且如下所示会出现异常
org.springframework.expression.spel.SpelEvaluationException:EL1008E:(pos 0):在'org.springframework.cache.interceptor.CacheExpressionRootObject'类型的对象上找不到属性或字段'mykey' - 可能不公开?
请建议.
Rub*_*ben 39
看来Spring不允许你为缓存键提供静态文本SPEL,并且它不包含默认键上方法的名称,因此,你可能处于两种方法使用时的情况相同cacheName且没有密钥可能会使用相同的密钥缓存不同的结果.
最简单的解决方法是提供方法的名称作为键:
@Cacheable(value="usercache", key = "#root.methodName")
public string sayHello(){
return "test"
}
Run Code Online (Sandbox Code Playgroud)
这将sayHello成为关键.
如果您确实需要静态键,则应在类中定义静态变量,并使用#root.target:
public static final String MY_KEY = "mykey";
@Cacheable(value="usercache", key = "#root.target.MY_KEY")
public string sayHello(){
return "test"
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到可在密钥中使用的SPEL表达式列表.
小智 16
尝试添加单引号mykey.这是一个SPEL表达式,单打引号String再次成为一个表达式.
@Cacheable(value="usercache", key = "'mykey'")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14839 次 |
| 最近记录: |