1

我正在尝试将字符串缓存抽象机制与 guice 模块一起使用。我创建了拦截器:

CacheManager cacheManager = createCacheManager(); 绑定(CacheManager.class).toInstance(cacheManager);

    AppCacheInterceptor interceptor = new AppCacheInterceptor(
            cacheManager,
            createCacheOperationSource()
    );

    bindInterceptor(
            Matchers.any(),
            Matchers.annotatedWith(Cacheable.class),
            interceptor
    );

    bindInterceptor(
            Matchers.any(),
            Matchers.annotatedWith(CacheEvict.class),
            interceptor
    );

然后,实现了Strings Cache接口和CacheManager,最后用@Cachable和@CacheEvict注解了我的DAO类:

public class DaoTester {

QssandraConsumer qs;

@CachePut(value = "cached_consumers", key = "#consumer.id")
public void save(QssandraConsumer consumer) {
    qs = consumer;
}

@Cacheable(value = "cached_consumers")
public QssandraConsumer get(String id) {
    if (id != null) {
        qs.getId();
    }
    return qs;
}

@CacheEvict(value = "cached_consumers", key = "#consumer.id")
public void remove(QssandraConsumer consumer) {
    qs = consumer;
}}

缓存很好 - 这里没有问题,但是当我尝试驱逐(在这个例子中调用 remove 方法)时,evrything 崩溃了,我看到:

线程“main”中的异常 org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 10): 在 org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference .java:205) 在 org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57) 在 org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:72) 在 org. springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93) 在 org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:88) 在 org.springframework.cache.interceptor.ExpressionEvaluator。 org.springframework 中的键(ExpressionEvaluator.java:80)。cache.interceptor.CacheAspectSupport$CacheOperationContext.generateKey(CacheAspectSupport.java:464) at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheEvicts(CacheAspectSupport.java:260) at org.springframework.cache.interceptor.CacheAspectSupport.inspectAfterCacheEvicts(CacheAspectSupport. java:232) at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:215) at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66) at qiwi.qommon.deployment.dao .DaoTester.main(DaoTester.java:44) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java :25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

这里有什么问题?!顺便说一句,缓存的对象是:

public class QssandraConsumer implements Identifiable<String> {
private String id;
private String host;

@Override
public String getId() {
    return id;
}

@Override
public void setId(String id) {
    this.id = id;
}

public String getHost() {
    return host;
}

public void setHost(String host) {
    this.host = host;
}

@Override
public boolean equals(Object object) {
    if (this == object) {
        return true;
    }
    if (null == object) {
        return false;
    }


    if (!(object instanceof QssandraConsumer)) {
        return false;
    }

    QssandraConsumer o = (QssandraConsumer) object;

    return
        Objects.equal(id, o.id)
            && Objects.equal(host, o.host);
}

@Override
public int hashCode() {
    return Objects.hashCode(
        id, host
    );
}

@Override
public String toString() {
    return Objects.toStringHelper(this)
        .addValue(id)
        .addValue(host)
        .toString();
}

}

4

1 回答 1

0

最后我弄清楚问题的原因是什么:当注入一个使用注释的类时(被截获,比如@Cachableor @CacheEvict)Guice 增强了类(AOP 在运行时进行字节码修改)。因此,当CacheInterceptor尝试评估key = "#consumer.id"它失败时,因为在增强类中找不到参数名称(请参阅:)LocalVariableTableParameterNameDiscoverer#inspectClass。所以它不能在 Guice 开箱即用。在春天创建了代理类 - 所以这里没有问题。

于 2013-04-08T17:06:18.603 回答