Spring AOP can't double bind annotation

2019-07-14 15:09发布

问题:

I have annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Retry {

    int DEFAULT_RETRIES = 2;

    int times() default DEFAULT_RETRIES;
}

Which is used either on class level:

@Retry(times = 5)
public class PersonServiceClass {

//...

    public void deletePerson(long id) {
        //...
    }
}

Or method level (another class, not PersonServiceClass):

@Retry
public void deletePerson(long id) {
    //...
}

The aspect is caught by such class:

@Aspect
@Component
public class RetryInterceptor {

    @Around("@within(retry) || @annotation(retry)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint, Retry retry) throws Throwable {
        System.out.println("around - " + retry);
        System.out.println("joinpoint - " + proceedingJoinPoint);
        return aroundHandler(proceedingJoinPoint, retry);
    }

And aspect is correctly caught on method or class level, but there is something wrong with binding Retry annotation.

When @Around is as following: @Around("@within(retry) || @annotation(retry)") then:

  1. When caught on method level than retry is binded
  2. When caught on class level than retry is null.

When @Around is as following @Around("@annotation(retry) || @within(retry)") then:

  1. When caught on method level than retry is null.
  2. When caught on class level than retryis binded.

Spring Boot Parent Version - 2.1.1.RELEASE

回答1:

...now you challenged me:) and i could reproduce the issue!

Pragmatically I (would) solve(d) it like that:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ExampleAspect {

  @Around("@within(retry)")
  public Object typeAspect(ProceedingJoinPoint joinPoint, Retry retry) throws Throwable {
    return commonAspect(joinPoint, retry);
  }

  @Around("@annotation(retry)")
  public Object methodAspect(ProceedingJoinPoint joinPoint, Retry retry) throws Throwable {
    return commonAspect(joinPoint, retry);
  }

  private Object commonAspect(ProceedingJoinPoint joinPoint, Retry retry) throws Throwable {
    System.out.println("Retry is :" + (retry == null ? "null" : retry.value()));
    // ... do your (common) stuff here
    return proceed;
  }

}

..welcome! :-)

And since you already have a (common) aroundHandler() method, it comes down to "introducing 2 public facades/PCDs for it".

Additional hint: Rename times() (if its the only/main property of that annotation) to: value()! ..then you can do "just" @Retry(100).