切入点混淆与继承 [英] Pointcut confusion with inheritance

查看:83
本文介绍了切入点混淆与继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为编写与方法的所有执行匹配的切入点而感到困惑.我尝试了应该与类Alpha的所有方法执行匹配的切入点:

I am confused by writing a pointcut that matches all executions of a method. I tried the pointcut that should match all method-executions of class Alpha:

execution(* Alpha.*(..))

具有以下等级的阶级

public class Alpha {
    public void alphaMethod() {...}
}
public class Beta extends Alpha {
    public void betaMethod() {
        alphaMethod();
    }
}

如果主程序在Beta实例上调用alphaMethod,则我的建议像预期的那样被调用,但是主程序调用了在我的建议内也调用alphaMethodbetaMethod,但没有被调用,我不这样做.不明白为什么.

If the Main-program calls alphaMethod on an Beta-instance my advice is called like expected but the Main-program calls betaMethod that also calls alphaMethod inside my advice is not called and I don't understand why.

方面定义:

@Aspect
public class MyAspect {
    @Before(value = "execution(* Alpha.*(..))", argNames="joinPoint")
    public void myAdvice(JoinPoint joinPoint) {
        System.out.println("BEFORE: " + joinPoint.getSignature());
    }
}

主要方法:

Beta beta = ...;
beta.alphaMethod(); //advice is called
beta.betaMethod(); //advice is NOT called.

推荐答案

这是预期的结果.

Spring AOP使用代理类包装建议的bean.当您从Beta方法中调用alphaMethod()时,代理甚至不知道它.

Spring AOP uses proxy classes to wrap advised beans. When you call alphaMethod() from within a Beta method, the proxy isn't even aware of it.

请参见 答案以获取更多信息.

See this answer for more information.

这篇关于切入点混淆与继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆