I'm using Spring 3.1 and I want to use the new cache features. Then, I tried:
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
<!-- Ehcache library setup -->
<bean id="ehcache"
cla开发者_如何学Css="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:config-location="classpath:ehcache.xml" />
But I didn't find the way to configure my custom KeyGenerator. Any idea?
There is a better way in Spring 3.1 RC1:
<cache:annotation-driven key-generator="myKeyGenerator"/>
<bean id="myKeyGenerator" class="com.abc.MyKeyGenerator" />
import org.springframework.cache.interceptor.KeyGenerator;
public class MyKeyGenerator implements KeyGenerator {
public Object generate(Object target, Method method, Object... params) {
}}
As of today just delete the org.springframework.context.support-3.1.0.RC1.jar\org\springframework\cache\config\spring-cache-3.1.xsd from the jar file you get when you download spring and it works fine.
Ok, I just find a way to do this...
<!-- <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" />
As you can see, I use the AnnotationDrivenCacheBeanDefinitionParser, I put the configuration in my xml, and it works :) Done!
edit:
For Spring > 3.2, you can use a simple Java class configuration implementing CachingConfigurer:
@EnableCaching(mode = AdviceMode.ASPECTJ)
public class CacheConfig implements CachingConfigurer {
public KeyGenerator keyGenerator() {
return new ReflectionBasedKeyGenerator();
}
public CacheManager cacheManager() {
return new RedisCacheManager(redisCacheTemplate);
}
}
I encountered a problem with Spring Frameworks default Cache KeyGenerator. It seems to often encounter conflicts and it appears to have been recorded on this issue
I know this question has already been marked as answered, but I thought I would share how I resolved this...
<cache:annotation-driven cache-manager="cacheManager" key-generator="entityKeyGenerator" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:/ehcache-dciComponent.xml" p:shared="true" />
Basically, we created and used our own Cache KeyGenerator in place of the default one.
精彩评论