当@Cacheable在缓存中找到资源时,不执行@Before建议

时间:2016-11-23 10:30:13

标签: java spring aop spring-aop spring-cache

所以,我试图在我的spring缓存中添加一个日志记录方面,每次在 @Cacheable 注释之前执行(至少我希望)。

我有我的DAO对象,其中某些方法具有 @Cacheable 注释:

@Cacheable(value = "BalanceGroupCahce", key = "#balanceGroupId")
 public BalanceGroup fetchBalanceGroup(Integer userId, Integer balanceGroupId) {
     //code to fetch balanceGroup from database;
     return balanceGroup;
  }

我还有一个方面定义 @Pointcut ,如下所示:

@Pointcut(value="@annotation(org.springframework.cache.annotation.Cacheable)")
public void cacheablePointcut(){}

@Before 建议会将注释作为参数获取,并将记录器附加到相应的缓存中:

@Before(value="cacheablePointcut() && @annotation(annotation)")
 public void addLoggerForCacheable(JoinPoint joinPoint, Cacheable annotation){
     //code to add the logger
 }

所以发生的事情是,只要在缓存中找不到请求的资源,一切正常。当在缓存中找到资源时,既不执行方法也不执行 @Cacheable 注释。 这意味着我的建议也没有被触发。 我知道如果spring在缓存中找到资源,则不会执行该方法,但我认为 @Cacheable 注释仍然会执行我的切入点。

但显然没有执行 @Cacheable 注释,这意味着其他人正在检查资源是否在缓存中。

所以问题是我怎样才能重写 @Pointcut ,以便每次测试/更新/逐出缓存时执行? 另外,我很好奇谁在测试资源是否在缓存中?

0 个答案:

没有答案