简体   繁体   中英

Spring Annotation based AOP to intercept method implemented in parent class

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" />

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() {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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