【问题标题】:Pointcut for Inherited methods with super method calls具有超级方法调用的继承方法的切入点
【发布时间】:2019-11-13 11:48:27
【问题描述】:

我有以下课程

class A {
    public void someone() {
        helpMe();
    }
    private void helpMe() {
        // do something here
    }
}
class B extends A {
    public void help() {
        super.someone();
    }
}
class C extends A {
    public void me() {
        super.someone();
    }
}

所以我想在每次调用 helpMe 方法时做一些事情。 A.helpMe() 永远不会被显式调用。对A.helpMe() 的所有方法调用都是通过A.someone() 调用的,后者通过B.help()C.me() 进一步调用。

helpMe 包含所有其他类都需要的通用实现。

我尝试过的切入点

execution(* A.helpMe(..)) // does not work
execution(* A+.helpMe(..)) // does not work
execution(* *.helpMe(..)) // does not work

execution(* A.*(..)) // does not work
execution(* B.someone(..)) // does not work
execution(* A+.*(..)) // forms a point cut for B.help() and C.me() only
execution(* *.*(..)) // forms a point cut for B.help() and C.me() only
execution(* B.*(..)) // forms a point cut for B.help() only

我在某处读到pointcuts for super 是不允许的。如果是这样,有哪些有效的解决方法?

我尝试使用annotations 获取pointcut,但它也不起作用。

【问题讨论】:

  • Spring AOP 通过代理应用,内部方法调用不能被拦截。为此,您需要一个完整的 AOP 解决方案,例如 AspectJ,具有加载或编译时编织。

标签: java spring-boot aop aspectj pointcut


【解决方案1】:

1 如果有疑问,请始终退回到完全硬编码的切入点并再次继续工作:

execution(private void helpMe())

你确定你是 using AspectJ 而不是普通的 Spring AOP(它不能建议私有方法)吗?


仅供参考:https://www.eclipse.org/aspectj/doc/next/progguide/language-joinPoints.html

call 连接点不会捕获对非静态方法的超级调用

...不适用于execution

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 2011-10-17
    • 2015-03-07
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多