为什么“Save方法”被拦截了,而“findAll”没有被拦截

问题描述 投票:0回答:3

问题很简单

@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
而不是实际的存储库。

java spring-boot spring-data-jpa spring-aop
3个回答
0
投票

假设存储库具有以下结构

public interface JpaEmployeeRepository extends CrudRepository<JpaEmployee, Long> {..}

以下切入点适用于这两种情况

@Around("execution(* org..*Repository.save(..))")

@Around("execution(* org..*Repository.findAll(..))")

如果我正确理解了这个问题,则要求是拦截特定包中特定方法的执行。如果是,可以在此处阅读有关相同内容的更多详细信息。 @AspectJ 包内所有方法的切入点


0
投票

此问题与 Spring 问题 27761 相同,并已在 AspectJ 1.9.20.1 中解决,另请参阅 AspectJ 问题 256

根本原因与桥接方法有关。


-1
投票

你使用 Spring-BOOT 吗? Spring-BOOT 中的方面仅在第一次调用类中的单个方法时调用建议。如果您希望 Spring-BOOT 在任何给定类中的多个方法的所有情况下都尊重您的 @Around 建议 - 您需要将该类作为 bean 访问(SB 的 @Bean)...

Spring-BOOT 的 AOP 与使用 AspectJ 的切面并不是 100% 相同——主要区别是 AspectJ 修改了 Spring 使用动态代理的字节码。

© www.soinside.com 2019 - 2024. All rights reserved.