Myk*_*nko 5 java spring spring-boot spring-cache
我已经实现了一个CacheInterceptor允许通过通配符逐出缓存的自定义:
public class CustomCacheInterceptor extends CacheInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomCacheInterceptor.class);
@Override
protected void doEvict(Cache cache, Object key) {
try {
// evict cache
} catch (RuntimeException ex) {
getErrorHandler().handleCacheEvictError(ex, cache, key);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我正在努力让它工作:
@Configuration
public class CustomProxyCachingConfiguration extends ProxyCachingConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(CustomProxyCachingConfiguration.class);
@Bean
public CacheInterceptor cacheInterceptor() {
LOGGER.info("Creating custom cache interceptor");
CacheInterceptor interceptor = new CustomCacheInterceptor();
interceptor.setCacheOperationSources(cacheOperationSource());
if (this.cacheResolver != null) {
interceptor.setCacheResolver(this.cacheResolver);
} else if (this.cacheManager != null) {
interceptor.setCacheManager(this.cacheManager);
}
if (this.keyGenerator != null) {
interceptor.setKeyGenerator(this.keyGenerator);
}
if (this.errorHandler != null) {
interceptor.setErrorHandler(this.errorHandler);
}
return interceptor;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我CustomCacheInterceptor的默认情况下被覆盖:
Overriding user-defined bean definition for bean 'cacheInterceptor' with a framework-generated bean definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=customProxyCachingConfiguration; factoryMethodName=cacheInterceptor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/package/test/CustomProxyCachingConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cache.annotation.ProxyCachingConfiguration; factoryMethodName=cacheInterceptor; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/cache/annotation/ProxyCachingConfiguration.class]]
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方法来解决这个问题:
1)试图排除ProxyCachingConfiguration用@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ProxyCachingConfiguration.class)),并提供自己的BeanFactoryCacheOperationSourceAdvisor-没有帮助
2) 尝试过,@Primary但似乎只在注射时有效 - 不是我的情况
3) 尝试选择不同的 bean 名称 - “customCacheInterceptor” - 在这种情况下,我的自定义类没有被调用
4)试图在我的配置之前添加@DependsOn("cacheOperationSource")哪个位于ProxyCachinConfiguration使Spring加载ProxyCachinConfiguration- 没有帮助
最奇怪的是,有时我的配置会在应用程序启动期间获胜,并且一切正常
如何CacheInterceptor用我的CustomCacheInterceptor?
Spring Boot 版本 - 2.0.0.RELEASE
Spring bean 覆盖很混乱,应该避免。给定名称的最后一个 bean 定义创建了实际的 bean,但定义之间没有可预测的顺序,因为它取决于多个因素,例如 Groovy、XML 和 JavaConfig。
ProxyCachingConfiguration从上下文中排除配置类并自己重新定义它会更安全@ComponentScan,同时提供CacheInterceptor和 BeanFactoryCacheOperationSourceAdvisor。
如果您的目标只是替换默认BeanFactoryCacheOperationSourceAdvisorbean 中设置的建议,您可以定义一个新BeanPostProcessorbean 并setAdvice()在 期间调用postProcessBeforeInitialization()。
| 归档时间: |
|
| 查看次数: |
748 次 |
| 最近记录: |