繁体   English   中英

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

[英]Custom Spring AOP Annotation Not Working for Default Method

我正在尝试在已经放置它的类中的新方法周围添加已经可用的 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 初学者错误,即使它另有明确记录(搜索术语“自调用”)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM