AOP中调用和执行之间的区别 [英] Difference between call and execution in AOP
问题描述
我试图尽可能简单地理解AOP中执行和调用之间的区别.根据我的收集,execution()将在执行代码中添加一个连接点,因此在这种情况下为HelloWorldSayer.sayHello()
,但是如果切入点为call()
,则连接点将为HelloWorldSayer.main()
.这是正确的吗?
I'm trying to understand the difference between execution and call in AOP as simply as possible. From what I gather, execution() will add a join point in the executing code, so HelloWorldSayer.sayHello()
in this case, but if the pointcut was call()
, then the join point will be HelloWorldSayer.main()
. Is this correct?
public class HelloWorldSayer {
public static void main (String[] args) {
sayHello();
}
public static void sayHello() {
System.out.println("Hello");
}
}
public aspect World {
public hello():
execution(static void HelloWorldSayer.sayHello());
after() hello() {
System.out.println("Bye");
}
}
推荐答案
如果再次查看HelloWorldSayer
类,将有4个连接点阴影(2个执行切入点和2个调用切入点).
If we look at the HelloWorldSayer
class again, there are 4 join point shadows (2 execution pointcuts and 2 call pointcuts).
换句话说,public static void main (String[] args)
和public static void sayHello()
指的是执行切入点. (HelloWorldSayer.)sayHello();
和System.out.println("Hello");
是指调用切入点.
In other words, public static void main (String[] args)
and public static void sayHello()
refer to the execution pointcut. (HelloWorldSayer.)sayHello();
and System.out.println("Hello");
refer to the call pointcut.
如果按如下所示更改声明的切入点,则切入点将选择sayHello();
If you change the declared pointcut as follows, the pointcut selects sayHello();
public pointcut hello():
call(static void HelloWorldSayer.sayHello());
另一方面,您可以如下更改声明的切入点,该切入点选择sayHello方法声明public static void sayHello()
.
On the other hand, you change the declared pointcut as follows, the pointcut selects the sayHello method declaration public static void sayHello()
.
public pointcut hello():
execution(static void HelloWorldSayer.sayHello());
最后,请阅读以下答案以更好地了解call()
和execution()
:
https://stackoverflow.com/a/18149106/904745
At last, please read this answer to get better understanding about call()
and execution()
:
https://stackoverflow.com/a/18149106/904745
这篇关于AOP中调用和执行之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!