Difference between call and execution in AOP

2019-02-25 21:50发布

问题:

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");
    }
}

回答1:

If we look at the HelloWorldSayer class again, there are 4 join point shadows (2 execution pointcuts and 2 call pointcuts).

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.

If you change the declared pointcut as follows, the pointcut selects sayHello();

public pointcut hello():
    call(static void HelloWorldSayer.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());

At last, please read this answer to get better understanding about call() and execution(): https://stackoverflow.com/a/18149106/904745



标签: java aop aspectj