java – spring aop截取org.springframework.cache.interceptor.CacheInterceptor #incoke

我尝试了以下代码,但它不起作用:

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

附:我确信CacheInterceptor是一个Spring托管bean.

最佳答案 经过一些实验,我发现用用户定义的CacheInterceptor替换内置的Spring可以解决我的问题.

这是代码,以防有人有类似的要求.

  @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,与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);
    }
  }

可以在ProxyCachingConfiguration类中找到CacheInterceptor bean中构建的spring.

希望能帮助到你.

点赞