2014-04-24 2 views
1

스프링 캐싱 작업을 시작했습니다. 내 서비스 방법은스프링 캐싱 키 생성기

... 키 절없이

@Override 
@Cacheable(value="profile", **key**="personId") 
public String cpuService2(Long personId, String personAddress){ 
    return "CachedMessage"; 
} 

는 예외를 던져 나는이 메서드를 호출 할 때 같은 예외가 발생, 캐싱 등 뿐만 키를 자동 생성하는 두 매개 변수를 가정하지 않습니다 ..

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) 

검색된 문서이지만 실마리가 없습니다. 희망이 누군가를이 문제를 해결합니다.

답변

7

당신은 personId

@Override 
@Cacheable(value="profile", key="#personId") 
public String cpuService2(Long personId, String personAddress){ 
    return "CachedMessage"; 
} 

caching abstraction chapter의 앞에 #을 놓치고있는 것은 당신이 뭘 하려는지의 수많은 사례가있다.

+0

감사 :

@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; } 

그런 다음 당신은 당신이 그것을 필요로 할이의 KeyGenerator에 연결할 수 있습니다. 잘못된 코드가있는 블로그를 언급하고있었습니다. – user3561481

4

상황이 더욱 복잡 해지면 SpEL은 키를 생성하는 데 사용 된 알고리즘을 쉽게 이해할 수있는 이상적인 솔루션이 아닙니다. 당신은 키를 생성하기 위해 자바를 사용하는 것이 좋습니다 :

@Cacheable(value = "nameofmycachearea", keyGenerator="fooGenerator")