尤慕李
尤慕李

Reputation: 435

spring aop that intercepts org.springframework.cache.interceptor.CacheInterceptor#invoke

I have tried the following code , but it does not work:

@Component
@Aspect
@Order(Integer.MAX_VALUE)
public class CacheAspect {

    @Around("execution(public * org.springframework.cache.interceptor.CacheInterceptor.invoke(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //CLASS_CACHE.set(signature.getReturnType());
        return joinPoint.proceed();
    }
}

P.S. I am sure CacheInterceptor is a spring managed bean.

Upvotes: 5

Views: 4604

Answers (1)

尤慕李
尤慕李

Reputation: 435

After some experimenting, I find that replacing the spring built in CacheInterceptor with a user defined one can solve my problem. Here is the code in case someone has similar requirments.

  @Configuration
  @EnableCaching
  @Profile("test")
  public class CacheConfig {
    @Bean
    @Autowired
    public CacheManager cacheManager(RedisClientTemplate redisClientTemplate) {
      return new ConcurrentMapCacheManager(redisClientTemplate, "test");
    }

    @Bean
    public CacheOperationSource cacheOperationSource() {
      return new AnnotationCacheOperationSource();
    }

    @Bean
    public CacheInterceptor cacheInterceptor() {
      CacheInterceptor interceptor = new MyCacheInterceptor();
      interceptor.setCacheOperationSources(cacheOperationSource());
      return interceptor;
    }
  }

MyCacheInterceptor.java, which shares the same logic with CacheAspect:

  public class MyCacheInterceptor extends CacheInterceptor {
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
      Method method = invocation.getMethod();
      //CLASS_CACHE.set(signature.getReturnType());
      return super.invoke(invocation);
    }
  }

The spring built in CacheInterceptor bean can be found in ProxyCachingConfiguration class.

Hope it helps.

Upvotes: 8

Related Questions