簡體   English   中英

基於Spring Annotation的AOP攔截在父類中實現的方法

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

我們使用基於注釋的AOP來攔截功能的方法。

基礎界面:

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擴展了許多類。 創建自定義注釋以識別點切割。

方面是

@Aspect
public class SomeAspect {

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

}

我們如何使用基於AOP的AOP攔截ActualImplemetation.someMethod (在父類中實現)?

使用aop配置可以通過以下方式實現

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

這應該適用於一些修改:

@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