2

我刚刚开始研究 Spring 缓存。

我的服务方式是...

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

没有 key 子句,不会抛出异常,并假定两个参数都自动生成以进行缓存,但使用 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)

搜索文档但没有线索。希望有人能解决这个问题。

4

2 回答 2

8

你错过了前面的#personId

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

缓存抽象一章有很多你想要做的例子。

于 2014-04-24T13:53:22.560 回答
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")
于 2015-01-13T20:53:22.773 回答