Java 拦截org.springframework.cache.interceptor.CacheInterceptor#invoke的spring aop
我尝试了以下代码,但不起作用:Java 拦截org.springframework.cache.interceptor.CacheInterceptor#invoke的spring aop,java,spring,aop,aspect,spring-cache,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) thro
@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。经过一些实验,我发现用用户定义的缓存拦截器替换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);
}
}
spring内置的CacheInterceptor
bean可以在ProxyCachingConfiguration
类中找到
希望有帮助。什么menas“不起作用”?@Jens
CacheInterceptor#invoke
无法被拦截。为什么要拦截和拦截@M.Deinum我想将方法信息收集到ThreadLocal中,并在其他地方使用。但您真的想拦截拦截器吗?此解决方案可帮助我在自定义拦截器中使用缓存提供程序put方法,使我能够在put时设置逐出时间,这不是spring缓存抽象的一部分,我在cacheInterceptor()bean方法中添加了@Primary,谢谢!