Your around()
advice正在拦截call
and execution
注释方法的连接点@WithStyle
(i.e., doIt()
)。如果您添加一个System.out.println(pjp);
对你而言:
@Aspect
public class StyleAspect {
@Around("@annotation(WithStyle) ")
public Object doItWithStyle(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(pjp);
System.out.println("Doing it in style...");
Object result;
try{
result = pjp.proceed();
}
finally{
System.out.println("Done");
}
return result;
}
}
你会得到以下输出:
call(public void SomeClass.doIt()) <----
Doing it in style...
execution(public void SomeClass.doIt()) <----
Doing it in style...
I'm doing it....
Done
Done
您可以清楚地看到连接点call
and execution
方法的SomeClass.doIt()
正在被拦截around
advice doItWithStyle
.
从拦截来看call
, the around
advice is weaving代码如下:
// around advice code before the pjp.proceed();
someClass.doIt(); // during the pjp.proceed();
// around advice code after the pjp.proceed();
最后:
System.out.println("Doing it in style...");.
someClass.doIt();
System.out.println("Done");
从执行来看:
@WithStyle
public void doIt() {
// around advice code before the pjp.proceed();
System.out.println("I'm doing it....");
// around advice code after the pjp.proceed();
}
最后:
@WithStyle
public void doIt() {
System.out.println("Doing it in style...");
System.out.println("I'm doing it....");
System.out.println("Done");
}
产生输出:
Doing it in style...
Doing it in style...
I'm doing it....
Done
Done
现在,如果您想避免around
advice拦截两者call
和execution
该方法的doIt()
。您需要进一步限制您的拦截的连接点around
advice。只是拦截该方法call
, 你可以做:
@Around("@annotation(WithStyle) && call(* *(..))")
对于方法execution
:
@Around("@annotation(WithStyle) && execution(* *(..))")
您可以通过调整方法的签名,根据方法的参数数量、返回类型、名称等进一步限制被拦截的连接点call
or execution
切入点。