sqljpaspring-data-jpaspring-aopaspect

Pointcut on annotated method


I'm trying to add a custom annotation for JPA repository methods to have a advice on @Query value.

Below is the piece of code I tried

MyFilter class

@Aspect
@Component
public class MyFilter {
   @Pointcut("execution(* *(..)) && @within(org.springframework.data.jpa.repository.Query)")
   private void createQuery(){}

   @Around("createQuery()")
   public void invoke(JointPoint jp) {
   }
}

The Respository code

@MyFilter
@Query(Select * ...)
MyObject findByNameAndClass(...)

So I keep getting error

createQuery() is never called At MyFilter

I'm trying to update the Query value using the advice.

What am I doing wrong?


Solution

  • To capture the annotation I often use this pattern:

     "execution(@AnnotationToCapture * *(..)) && @annotation(annotationParam)"
    

    Then in the proceeding method, you can have the annotation as parameter:

    (..., AnnotationToCapture annotationParam, ...)