问题
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