AOP中调用和执行的区别 [英] Difference between call and execution in AOP

查看:25
本文介绍了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屋!

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