Search code examples
javaspring-bootspring-data-jpaspring-aop

Save method is intercepted but findAll is not


The problem is simple

@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
}

Breaking head here!

Edit: A small breakthrough. I printed the target, it returned SimpleJpaRepository instead of the actual repository.


Solution

  • Assuming the repository is of the following construct

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

    following pointcut works on both the cases

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

    and

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

    If I understand the question correctly the requirement is to intercept on the execution of a particular method within a specific package.If yes, More details on the same can be read here. @AspectJ pointcut for all methods inside package