我刚刚开始使用Spring缓存。
我的服务方法是......
@Override
@Cacheable(value="profile", **key**="personId" )
public String cpuService2(Long personId, String personAddress){
return "CachedMessage";
}
没有key子句,不抛出异常并假设两个参数都自动生成缓存 但是使用键,当我调用此方法时,抛出异常为......
Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'personId' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject'
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:246)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:107)
at org.springframework.expression.spel.ast.OpGT.getValueInternal(OpGT.java:37)
at org.springframework.expression.spel.ast.OpGT.getValueInternal(OpGT.java:29)
at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:98)
at org.springframework.cache.interceptor.ExpressionEvaluator.condition(ExpressionEvaluator.java:99)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.isConditionPassing(CacheAspectSupport.java:462)
at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.isConditionPassing(CacheAspectSupport.java:456)
at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheables(CacheAspectSupport.java:292)
at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:199)
at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy5.cpuService3(Unknown Source)
at pack100_cache.pack020CacheKey.TestSimpleBean.main(TestSimpleBean.java:34)
搜索文档但没有任何线索。希望有人能解决这个问题。
答案 0 :(得分:8)
您错过了personId
@Override
@Cacheable(value="profile", key="#personId" )
public String cpuService2(Long personId, String personAddress){
return "CachedMessage";
}
caching abstraction chapter有很多关于你要做的事情的例子。
答案 1 :(得分:5)
当事情变得更复杂时,当易于理解用于生成密钥的算法时,SpEL可能不是理想的解决方案。 您可以考虑使用Java生成密钥:
@Component
public class FooGenerator implements KeyGenerator {
@Override
public Object generate(Object target, Method method, Object... params) {
String param = "";
if (params.length == 2) {
if (params[0] instanceof String) {
param = (String) params[0];
String key = "FooKeyEquatesTo" + param;
return key;
}
然后你可以在你需要的地方连接这个密钥生成器:
@Cacheable(value = "nameofmycachearea", keyGenerator="fooGenerator")