2013-04-06 48 views
0

我試圖在Guice模塊中使用字符串緩存抽象機制。 我創建了攔截器:不能從Guice中的Spring Cache中驅逐DI應用程序

CacheManager cacheManager = createCacheManager(); bind(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 
    ); 

然後,執行字符串緩存接口和CacheManager的,最後我的註釋DAO類與@Cachable和@CacheEvict:

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

緩存僅僅是罰款 - 沒有問題在這裏,但是當我嘗試驅逐(調用在本例中除去方法),evrything崩潰和我看到:

異常在線程 「主」 org.springframework.expression.spel.SpelEvaluationException:EL1007E:(POS 10):字段或屬性「ID 'ca n不是被上空 在org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:205) 在org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:72)發現 at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:57) at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:93) at org.springframework.expression .spel.standard.SpelExpression.getValue(SpelExpression.java:88) at org.springframework.cache.interceptor.ExpressionEvaluator.key(ExpressionEvaluator.java:80) at org.springframework.cache.interceptor.CacheAspectSupport $ CacheOperationContext.generateKey (CacheAspectSupport.java:464) 個在org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheEvicts(CacheAspectSupport.java:260) 在org.springframework.cache.interceptor.CacheAspectSupport.inspectAfterCacheEvicts(CacheAspectSupport.java:232) 在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(本機方法) 在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)

什麼是錯在這裏? BTW,緩存的對象是:

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(); 
} 

}

回答

0

最後我想通了什麼問題的原因: 注射使用的註釋(這被截獲,像@Cachable@CacheEvict)類時吉斯增強類(AOP使運行時字節碼修改)。因此,當CacheInterceptor試圖評估key = "#consumer.id"時,它失敗了,因爲在增強類中找不到參數名稱(請參閱:LocalVariableTableParameterNameDiscoverer#inspectClass)。 因此它不會在Guice開箱即可使用。 在春天,代理類被創建 - 所以這裏沒有問題。

相關問題