7

I'm using Spring Data in my project. So, I need to intercept some methods (save and delete) only from some entities only. I tried to configure the pointcut for my own repositories interfaces, but without success. The methods wasn't intercepted.

So, I found a solution that was to try to use the Spring CrudRepository interfaces into my 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) {...}
}

That was a good aproach! The interceptor was executed sucessfully! But my scenario is a little bit different. I have about 20 interfaces that extends JPARepository inteface from Spring Data, then i need to intercep 15 of them. The other ones, I can't.

Thus, my question is: Is there a way to intercept some methods of my own interfaces that extends JPARepository interface from Spring Data, using AOP or anything else?

Thank's in advance for any help!

Luciano Nunes
  • 55
  • 2
  • 9
  • 2
    have you found the answer? – VB_ May 15 '18 at 14:23
  • 2
    Yes, @VolodymyrBakhmatiuk, I resolved this issue putting the interface methods on the "@Pointcut" annotation, this way: @Pointcut("execution(* br.com.unilever.eddie.dao.*.save(*)) || " + "execution(* br.com.unilever.eddie.dao.*.saveAndFlush(*)) && " + "!execution(* br.com.unilever.eddie.dao.LoginDAO..*(..))") public void whenSaveOrUpdate() {}; @Pointcut("execution(* br.com.unilever.eddie.dao.*.delete(*))") public void whenDelete() {}; – Luciano Nunes May 16 '18 at 14:48
  • 2
    @LucianoNunes Please put what you've found as an answer so that anyone can see it. – Jin Kwon Mar 06 '20 at 06:44

0 Answers0