[英]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.