简体   繁体   English

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

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

I'm trying to add already working AOP annotation around a new method in the class where it is already placed.我正在尝试在已经放置它的类中的新方法周围添加已经可用的 AOP 注释。 Its not working for the new method which I have defined as a default method to an interface (not working even when not overriden) and I'm unable to find the cause of the same.它不适用于我定义为接口默认方法的新方法(即使没有覆盖也不起作用),我无法找到相同的原因。 Code is代码是

@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

Interception working for execute(HoldRequest holdRequest) but not for doExecute(HoldRequest holdRequest) .拦截适用于execute(HoldRequest holdRequest)但不适用于doExecute(HoldRequest holdRequest) Please help me with the fix for this.请帮我解决这个问题。

This works flawlessly for me.这对我来说完美无缺。 The only explanation why doExecute(..) interception is not working for you is that you use self-invocation, eg like this: 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();
  }

It is a classical Spring AOP beginner's mistake to assume that this is working, even though it is clearly documented otherwise (search for the term "self-invocation").假设这是有效的,这是一个典型的 Spring AOP 初学者错误,即使它另有明确记录(搜索术语“自调用”)。

So the problem was not in the code you showed in your question, but it is in the code you chose to hide from us.因此,问题不在于您在问题中显示的代码,而在于您选择向我们隐藏的代码。 Please be advised to learn why an MCVE in every question is so important and ask better next time.请注意了解为什么每个问题中的MCVE如此重要,并在下次提出更好的问题。 Thank you.谢谢你。

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

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