当前位置: 代码迷 >> ASP >> aspectj中call跟execution的区别
  详细解决方案

aspectj中call跟execution的区别

热度:170   发布时间:2012-07-25 09:43:05.0
aspectj中call和execution的区别

? call execution 的指示符分别为 call Method-Signature )、 execution Method-Signature ),匹配方法签名的方法或构造函数的执行。 对于 call 来说,调用的连接点位于方法调用点的调用代码处;对于 execution 来说,执行的连接点位于方法执行的位置。也就是说, call execution 的重要区别在于它们传递了哪些类型给 AspectJ 编译器以用来与 aspect 进行链接。

?? 通常,我们在使用 call execution 时,从效果上来看并不会觉察出二者的区别。下面给出一个例子说明 call execution 的运行时机及区别。

MyAspectj中的代码:

public aspect MyAspectj {

? pointcut callF(): call(* f(..)); ?
? before() : callF() { ?
??? System.out.println("before call f()");
??? System.out.println(thisJoinPoint.getThis());
??? System.out.println(thisJoinPoint.getTarget());
? }
}

Test中的代码:

public class Test {

??? public void foo(){?
??? ? f();
??? }
??? public void f(){?
??? ? System.out.println("CflowTest.f();");?
??? }
}

Test1中的代码:

public class Test1 {
??? public void foo1(Test test){?
??? ??? ? test.f();
??? }?
??? public static void main(String[] args) {

??? ??? Test test=new Test();
??? ??? test.foo();
??? ??? System.out.println("**************************************");
??? ??? new Test1().foo1(test);??
??? }
}

?

Test1中main函数的运行结果:

before call f()
com.gaojian.aspectj.test.Test@141d683
com.gaojian.aspectj.test.Test@141d683
CflowTest.f();
**************************************
before call f()
com.gaojian.aspectj.test.Test1@16a55fa
com.gaojian.aspectj.test.Test@141d683
CflowTest.f();

??? ?

?

? ??? 从输出可以看到,在 execution 中, this target 指向同一个类 。在 call 中, this target 不是指向同一个类

???? execution call 还有一点很重要的区别。对于继承类来说,如果它没有覆盖父类的方法,那么 execution 不会匹配子类中没有覆盖父类的方法。比如说我们有一个类 B 继承于 A ,但没有覆盖 A 类的 foo() ,那么对于 B 的实例的 foo() 方法, execution(* B.foo()) 将不会被匹配。

???? 做个总结,如果想跟踪连接点的内部代码运行情况可以考虑使用 execution ,但如果你只关心连接点的签名(比如你使用第三方库或是标准 API ),则使用 call

  相关解决方案