如何为Spring Cache设置自定义KeyGenerator?
我正在使用Spring 3.1,并且想使用新的缓存功能。然后,我尝试:
<cache:annotation-driven /><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<!-- Ehcache library setup -->
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
但是我没有找到配置自定义KeyGenerator的方法。任何想法?
回答:
好的,我只是找到一种方法来做…
<!-- <cache:annotation-driven /> --><bean id="annotationCacheOperationSource"
class="org.springframework.cache.annotation.AnnotationCacheOperationSource" />
<bean id="cacheInterceptor" class="org.springframework.cache.interceptor.CacheInterceptor"
p:cacheDefinitionSources-ref="annotationCacheOperationSource"
p:cacheManager-ref="cacheManager" p:keyGenerator-ref="keyGenerator" />
<bean id="beanFactoryCacheOperationSourceAdvisor"
class="org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor"
p:adviceBeanName="cacheInterceptor" p:cacheDefinitionSource-ref="annotationCacheOperationSource" />
<bean id="keyGenerator"
class="my.company.cache.ReflectionBasedKeyGenerator" />
如您所见,我使用AnnotationDrivenCacheBeanDefinitionParser,将配置放入xml中,并且可以::完成!
编辑:
对于Spring> 3.2,可以使用实现CachingConfigurer的简单Java类配置:
@EnableCaching(mode = AdviceMode.ASPECTJ)public class CacheConfig implements CachingConfigurer {
public KeyGenerator keyGenerator() {
return new ReflectionBasedKeyGenerator();
}
public CacheManager cacheManager() {
return new RedisCacheManager(redisCacheTemplate);
}
}
以上是 如何为Spring Cache设置自定义KeyGenerator? 的全部内容, 来源链接: utcz.com/qa/433120.html