Annotation的AOP拦截父类实现的方法

Annotation的AOP拦截父类实现的方法

本文介绍了基于Spring Annotation的AOP拦截父类实现的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用基于注解的 AOP 来拦截功能.

We are using annotation based AOP for methods to intercept functionality.

基础界面:

public interface BaseInterface {
    public void someMethod();
}

抽象基础实现:

public abstract class AbstractBaseImplementation implements BaseInterface {
    public void someMethod() {
       //some logic
    }
}

子界面:

public interface ChildInterface extends BaseInterface {
  public void anotherMethod();
}

实现类

public class ActualImplemetation extends AbstractBaseImplementation implements ChildInterface {

   public void anotherMethod() {
     // Some logic
   }
}

AbstractBaseImplementation 扩展了许多类.创建自定义注释以识别切点.

There are many classes extended from the AbstractBaseImplementation. Custom Annotation is created for identifying the point cuts.

方面是

@Aspect
public class SomeAspect {

  @Before("@annotation(customAnnotation)")
  public void someMethod(JoinPoint joinPoint) {
     //Intercept logic
  }

}

我们如何使用基于注解的 AOP 拦截ActualImplemation.someMethod(在父类中实现)?

How can we intercept ActualImplemetation.someMethod (Which is implemented in the parent class) using Annotation based AOP?

使用aop配置可以通过

Using aop configuration this can be achieved by

<aop:advisor pointcut="execution(* com.package..*ActualImplemetation .someMethod(..))" advice-ref="someInterceptor" />

推荐答案

类似:

@Pointcut("execution(* com.package.*ActualImplemetation.someMethod(..))"
// OR
// using BaseInterface reference directly, you can use all sub-interface/sub-class methods
//@Pointcut("execution(* com.package.BaseInterface.someMethod(..))"
logMethod() { //ignore method syntax
 //.....
}

这篇关于基于Spring Annotation的AOP拦截父类实现的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:16