Reputation: 21
I just have started working on Spring caching.
My service method is...
@Override
@Cacheable(value="profile", **key**="personId" )
public String cpuService2(Long personId, String personAddress){
return "CachedMessage";
}
Without key clause, does not throw exception and assumes both parameters to autogenerate for cacheing but with key, When I call this method, throws exception as...
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)
Searched documentation but no clue. Hope somebody will address this concern.
Upvotes: 2
Views: 10511
Reputation: 10953
When things get more complicated SpEL perhaps is not the ideal solution when it comes to easy comprehension of the algorithm used to generate the key. You might consider to use Java for generating the key:
@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;
}
Then you can wire in this keygenerator where you need it:
@Cacheable(value = "nameofmycachearea", keyGenerator="fooGenerator")
Upvotes: 5
Reputation: 33091
You're missing the # in front of personId
@Override
@Cacheable(value="profile", key="#personId" )
public String cpuService2(Long personId, String personAddress){
return "CachedMessage";
}
The caching abstraction chapter has numerous examples of what you're trying to do.
Upvotes: 9