问题很简单
@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
而不是实际的存储库。
假设存储库具有以下结构
public interface JpaEmployeeRepository extends CrudRepository<JpaEmployee, Long> {..}
以下切入点适用于这两种情况
@Around("execution(* org..*Repository.save(..))")
和
@Around("execution(* org..*Repository.findAll(..))")
如果我正确理解了这个问题,则要求是拦截特定包中特定方法的执行。如果是,可以在此处阅读有关相同内容的更多详细信息。 @AspectJ 包内所有方法的切入点
此问题与Spring问题27761相同,并在AspectJ 1.9.20.1中解决,另请参阅AspectJ问题256。
根本原因与桥接方法有关。
你使用 Spring-BOOT 吗? Spring-BOOT 中的方面仅在第一次调用类中的单个方法时调用建议。如果您希望 Spring-BOOT 在任何给定类中的多个方法的所有情况下都尊重您的 @Around 建议——您需要将该类作为 bean 访问(SB 的 @Bean)...
Spring-BOOT 的 AOP 与使用 AspectJ 的切面并不是 100% 相同——主要区别是 AspectJ 修改了 Spring 使用动态代理的字节码。