Spring Annotation based AOP to intercept method implemented in parent class

送分小仙女□ 提交于 2019-12-08 11:08:44

问题


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

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. 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?

Using aop configuration this can be achieved by

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

回答1:


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



回答2:


This should work with some modification:

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


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


来源:https://stackoverflow.com/questions/12670617/spring-annotation-based-aop-to-intercept-method-implemented-in-parent-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!