Home > Mobile >  Check annotation value in Spring Pointcut Expression
Check annotation value in Spring Pointcut Expression

Time:10-19

I'm trying to add an aspect that is only applied on 'non- readonly transactions', so not for @Transactional(readonly=true)

This below code is applied for all methods that have an @Transactional annotation above it.

@AfterReturning("@annotation(org.springframework.transaction.annotation.Transactional)")
public void afterTransaction() {...}

There is an alternative by getting the information from the JoinPoint but it is cumbersome and I'd rather not go down that path.

The documentation seems not very specific about this.

CodePudding user response:

You can use an execution pointcut in order to limit annotation matching to joinpoints where the annotation has certain parameter values.

Simple annotation value matching

@AfterReturning("execution(@org.springframework.transaction.annotation.Transactional(readOnly=true) * *(..))")
public void process(JoinPoint joinPoint) {
  System.out.println(joinPoint);
}

Annotation value matching annotation parameter binding

@AfterReturning("execution(@org.springframework.transaction.annotation.Transactional(readOnly=true) * *(..)) && @annotation(transactional)")
public void process(JoinPoint joinPoint, Transactional transactional) {
  System.out.println(joinPoint   " -> "   transactional);
}
  • Related