我已经尝试了下面的代码,但是它不起作用:
@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/