【问题标题】:@Before AOP not triggering to method with @Cacheable annotation@Before AOP 不会触发带有 @Cacheable 注释的方法
【发布时间】:2021-02-17 17:34:57
【问题描述】:

我创建了一个将计算我的服务调用的 Aspect,它对所有方法都有效,除了一个带有 @Cacheable 注释的方法。我认为问题是因为我使用了@Around 建议,但在将其更改为@Before 后,同样的问题仍然存在。任何解决方法?

这是我的建议方法。

@Autowired
private CounterService counterService;

@Before("execution(* DepartmentService.*(..))")
public void increment(JoinPoint joinPoint) throws Throwable {
counterService.increment(joinPoint.getSignature().toShortString());
}

这是唯一在 Service 类中不起作用的方法。

@Override
@Cacheable(value = "departments", key = "#id")
public Department findOne(Long id) {
return departmentRepository.findOne(id);
}

【问题讨论】:

  • 如果结果被缓存,则不会调用该方法,因此您的建议将永远不会被执行。我怀疑它只会触发一次。
  • 是的,但是如何强制它拦截该方法?
  • 使用@Ordered切换方面的顺序,并明确设置<cache:annotation-driven />的顺序。但是你基本上有错误的日志记录,因为该方法实际上没有被调用,因为它是缓存中的一个值。
  • Holycow...你就是那个男人!你救了我的一天!请将此作为答案发布。非常感谢!

标签: java spring caching aop


【解决方案1】:

Cacheable 已经在方法周围注入了一个通知,这使得对实际方法的调用不会被调用,因此你的通知不会被调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    相关资源
    最近更新 更多