简体   繁体   English

基于Spring Annotation的AOP拦截在父类中实现的方法

[英]Spring Annotation based AOP to intercept method implemented in parent class

We are using annotation based AOP for methods to intercept functionality. 我们使用基于注释的AOP来拦截功能的方法。

Base interface: 基础界面:

public interface BaseInterface { 
    public void someMethod();
}

Abstract Base implementation: 摘要基础实施:

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

Child interface: 子界面:

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

Implementation Class 实施类

public class ActualImplemetation extends AbstractBaseImplementation implements ChildInterface {

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

There are many classes extended from the AbstractBaseImplementation . AbstractBaseImplementation扩展了许多类。 Custom Annotation is created for identifying the point cuts. 创建自定义注释以识别点切割。

Aspect is 方面是

@Aspect
public class SomeAspect {

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

}

How can we intercept ActualImplemetation.someMethod (Which is implemented in the parent class) using Annotation based AOP? 我们如何使用基于AOP的AOP拦截ActualImplemetation.someMethod (在父类中实现)?

Using aop configuration this can be achieved by 使用aop配置可以通过以下方式实现

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

Something like : 就像是 :

@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
 //.....
}

This should work with some modification: 这应该适用于一些修改:

@Pointcut("execution(@CustomAnnotation * *(..))")
public void customAnnotationAnnotatedMethods() {/**/}   


@Before("customAnnotationAnnotatedMethods()")
public void adviceBeforeCustomAnnotation() {
    ...
}

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

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