自定义 Spring AOP 注释不适用于默认方法

Custom Spring AOP Annotation Not Working for Default Method

我正在尝试在 class 已经放置的新方法周围添加已经工作的 AOP 注释。它不适用于我定义为接口默认方法的新方法(即使未被覆盖也无法正常工作)并且我无法找到相同的原因。代码是

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PaymentPlanLocking {}
@Aspect
@Component
public class PaymentPlanLockAspect
{
..
@Around("@annotation(PaymentPlanLocking)")
    public Object paymentPlanLocking(ProceedingJoinPoint joinPoint) throws Throwable
    {
..
public interface PaymentOrchestratorService<RQ, RS>
{

    /**
     * @param request to validate
     */
    void validate(RQ request) throws PaymentServiceException;

    /**
     * @param request to execute
     * @return response
     */
    RS execute(RQ request) throws PaymentServiceException;

    /**
     * @param request to execute
     * @return response
     */
    default RS doExecute(RQ request) throws PaymentServiceException{
        throw new RuntimeException("please override this method in subclass if using old model with execute-wrapped");
    }

}
@Service("holdPaymentService")
public class HoldPaymentOrchestrationService extends AbstractService<HoldResponse, HoldRequest>
        implements PaymentOrchestratorService<HoldRequest, HoldResponse>
{
...

@PaymentPlanLocking
    @Override
    public HoldResponse execute(HoldRequest holdRequest) throws PaymentServiceException

@PaymentPlanLocking
    @Override
    public HoldResponse doExecute(HoldRequest holdRequest) throws PaymentServiceException

拦截为 execute(HoldRequest holdRequest) 工作,但不为 doExecute(HoldRequest holdRequest) 工作。请帮我解决这个问题。

这对我来说完美无缺。为什么 doExecute(..) 拦截对你不起作用的唯一解释是你使用了自我调用,例如像这样:

  @PaymentPlanLocking
  @Override
  public HoldResponse execute(HoldRequest holdRequest) throws PaymentServiceException {
    return doExecute(holdRequest);
  }

  @PaymentPlanLocking
  @Override
  public HoldResponse doExecute(HoldRequest holdRequest) throws PaymentServiceException {
    return new HoldResponse();
  }

经典的 Spring AOP 初学者错误地认为这是有效的,即使它显然是 documented otherwise(搜索术语 "self-invocation")。

所以问题不在于您在问题中显示的代码,而在于您选择对我们隐藏的代码。请注意了解为什么每个问题中的 MCVE 如此重要,并在下次提出更好的问题。谢谢。