【发布时间】:2020-03-29 12:24:42
【问题描述】:
问题很简单
@Around("execution(* package.*Repository.save(..))")
public Object saveInterupt(ProceedingJoinPoint joinPoint) throws Throwable {
// This gets called whenever repository save is called
}
@Around("execution(* package.*Repository.findAll(..))")
public Object findInterupt(ProceedingJoinPoint joinPoint) throws Throwable {
// This IS NOT GETTING called whenever repository findAll is called
}
在这里打破头颅!
编辑:一个小的突破。我打印了目标,它返回 SimpleJpaRepository 而不是实际存储库。
【问题讨论】:
-
简单的问题,调试一天没解决。头疼!
-
CrudRepository中的方法findAll和save在SimpleJpaRepository中默认实现。所以*Repository是一个jdk动态代理,在运行时为了纪念SimpleJpaRepository。 -
@Lebecca 那么我如何记录实际的调用类?
-
您可以在
SimpleJpaRepository save() or getAll()方法中设置一个断点,并在调试模式下运行应用程序来证明我的话。关键不是内部对象,而是代理在调用内部对象方法之前执行的调用链。 Spring 使用一些缓存来构建链,这对我来说很神秘,为什么findAll没有得到增强。 -
@Around("execution(* org..*Repository.save(..)) 和 @Around("execution(* org..*Repository.findAll(..)) 为我工作一直以来。如果您想在包级别进行拦截,可以组合 && 执行中的切入点指示符。
标签: java spring-boot spring-data-jpa spring-aop