7

I've implemented a custom CacheInterceptor which allows evicting the cache by wildcard:

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

Now I'm trying to make it working:

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

The problem is that my CustomCacheInterceptor is being overridden by default one:

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]]

I've tried different ways to solve this problem:

1) Tried to exclude ProxyCachingConfiguration with @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ProxyCachingConfiguration.class)) and provide own BeanFactoryCacheOperationSourceAdvisor - didn't help

2) Tried @Primary but it seems to be working only at injection time - not my case

3) Tried to choose different name of the bean - "customCacheInterceptor" - in this case my custom class is not being invoked

4) Tried to add @DependsOn("cacheOperationSource") which is located in ProxyCachinConfiguration to make Spring loading ProxyCachinConfiguration before my config - didn't help

The most strange thing is that sometimes my config wins during application start and everything works fine

How can I override default CacheInterceptor with my CustomCacheInterceptor?

Spring Boot Version - 2.0.0.RELEASE

1
  • Such low-level override of this feature is not really expected. The configuration is low-level and beans are configured with role infrastructure so they are not supposed to be post-processed either. Rather than hacking a core feature, please roll out your own annotation and create your own interceptor. Mar 22, 2019 at 15:31

1 Answer 1

0

Spring bean overriding is messy and should be avoided. Last bean definition for given name creates the actual bean but there is no predictable order between definitions as it depends on multiple factors e.g. Groovy vs XML vs JavaConfig.

It would be safer to exclude the ProxyCachingConfiguration configuration class from your context @ComponentScan and redefine it yourself, providing both CacheInterceptor and BeanFactoryCacheOperationSourceAdvisor.

If your goal is to just replace the advice set in the default BeanFactoryCacheOperationSourceAdvisor bean you can define a new BeanPostProcessor bean and call setAdvice() during postProcessBeforeInitialization().

5
  • My goal is to make CustomCacheInterceptor working Mar 22, 2019 at 12:23
  • Define "working", it would depend how you use it e.g. do you auto-wire it by type. Mar 22, 2019 at 12:24
  • Replace default CacheInterceptor with my own Mar 22, 2019 at 12:24
  • I've tried to add @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = ProxyCachingConfiguration.class)) on my main class and it didn't help Mar 22, 2019 at 12:24
  • By default Spring invokes his own CacheInterceptor when proxying methods annotated with @Cacheable. I want to change the behaviour of this default CacheInterceptor - that's why I've created my own class Mar 22, 2019 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.