问题描述
我尝试了以下代码,但它不起作用:
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. 我确定 CacheInterceptor
是 Spring 管理的 bean.
P.S. I am sure CacheInterceptor
is a spring managed bean.
推荐答案
经过一些实验,我发现将 CacheInterceptor
中内置的 spring 替换为用户定义的 spring 可以解决我的问题.这是代码,以防有人有类似要求.
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,与 CacheAspect
共享相同的逻辑:
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);
}
}
CacheInterceptor
bean 中内置的 spring 可以在 ProxyCachingConfiguration
类中找到.
The spring built in CacheInterceptor
bean can be found in ProxyCachingConfiguration
class.
希望有帮助.
这篇关于拦截org.springframework.cache.interceptor.CacheInterceptor#invoke的spring aop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!