日期:2014-05-17  浏览次数:20904 次

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();

??? ?