【发布时间】: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...你就是那个男人!你救了我的一天!请将此作为答案发布。非常感谢!