问题描述
我为编写与方法的所有执行匹配的切入点而感到困惑.我尝试了应该与类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
,则我的建议像预期的那样被调用,但是主程序调用了在我的建议内也调用alphaMethod
的betaMethod
,但没有被调用,我不这样做.不明白为什么.
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.
这篇关于切入点混淆与继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!