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?
CodePudding user response:
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, ...)
CodePudding user response:
You should not use the @within
annotation for this purpose. Instead, you should use @annotation
, as follows:
@Pointcut("execution(* *(..)) && @annotation(org.springframework.data.jpa.repository.Query)")
private void createQuery(){}
Also, you should use JoinPoint to access the method signature and then you can extract the annotation from the signature.
CodePudding user response:
your class should look as follows
@Aspect
@Component
public class MyFilterAspect {
@Pointcut("execution(* *(..)) && @annotation(org.springframework.data.jpa.repository.Query)")
private void createQuery(){}
@Around("createQuery()")
public void applyFilter(ProceedingJoinPoint jp) {
}
}