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

时间:2015-12-16 09:16:25

标签: 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是一个弹簧托管bean。

1 个答案:

答案 0 :(得分:2)

经过一些实验,我发现用CacheInterceptor替换 @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; } } 内置的弹簧可以解决我的问题。 这是代码,以防有人有类似的要求。

CacheAspect

MyCacheInterceptor.java,与 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); } }

共享相同的逻辑
CacheInterceptor

可以在ProxyCachingConfiguration类中找到href="<%= asset_path('styles-bluegreen.css')%>" bean中内置的spring。

希望它有所帮助。