Spring AOP + JPAR存储库

Luc*_*nes 6 aop spring aspectj spring-data-jpa

我在项目中使用Spring Data。因此,我只需要从某些实体中拦截某些方法(保存和删除)。我尝试为自己的存储库接口配置切入点,但是没有成功。该方法未被拦截。

因此,我找到了一个尝试将Spring CrudRepository接口用于我的Pointcup的解决方案。

@Aspect
@Component
@Configurable
public class AuditLogAspect {

   @Pointcut("execution(* org.springframework.data.repository.CrudRepository+.save(*)) || " +
              "execution(* org.springframework.data.repository.CrudRepository+.saveAndFlush(*))")
    public void whenSaveOrUpdate() {};

   @Pointcut("execution(* org.springframework.data.repository.CrudRepository+.delete(*))")
   public void whenDelete() {};

   @Before("whenSaveOrUpdate() && args(entity)")
   public void beforeSaveOrUpdate(JoinPoint joinPoint, BaseEntity entity) {...}
}
Run Code Online (Sandbox Code Playgroud)

这是一个很好的方法!拦截器成功执行!但是我的情况有些不同。我有大约20个接口,这些接口从Spring Data扩展了JPARepository接口,然后需要插入其中的15个接口。其他的,我做不到。

因此,我的问题是:是否可以使用AOP或其他方法来拦截我自己的接口的某些方法,这些方法将从Spring Data扩展JPARepository接口?

预先感谢您的任何帮助!