java - 拦截org.springframework.cache.interceptor.CacheInterceptor#invoke的spring aop

标签 java spring aop aspect spring-cache

我已经尝试了下面的代码,但是它不起作用:

@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. 我确信 CacheInterceptor 是一个 spring 管理的 bean。

最佳答案

经过一些试验,我发现将CacheInterceptor 中内置的spring 替换为用户定义的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 类中找到 spring 内置的 CacheInterceptor bean。

希望对您有所帮助。

关于java - 拦截org.springframework.cache.interceptor.CacheInterceptor#invoke的spring aop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34308003/

相关文章:

java - 如何解决 Sonar 问题 "Remove this call to "等待“或将其移至 "while"循环”?

java - Java String 真的是不可变的吗?

java - Spring Boot - 远程 Zookeper 配置

java - 在 MappedSuperclass 上更新 Spring-data-jpa 时出错

ioc-container - Ninject:为 AoP 代理类型时可以使用注入(inject)构造函数吗?

spring - Grails 3.0.11 AOP 注释在 Controller 方法之前预处理命令对象

java - Java 中的常规字符串到 IDNA

java - 在 Hadoop Java API 中指定用户名和组?

java - Oracle DB - 对特定批处理的未初始化集合的 Java 引用

java - Spring aop与struts2