cflow的使用

cflow(pointcut):表示在执行连接点pointcut所匹配的函数时进行匹配,如下例所示:

aj中的代码:
package com.gaojian.aspectj.test;

public aspect MyAspectj {
pointcut callF(): execution(* f(..)) && cflow(call(* com.gaojian.aspectj.test1.*.foo(..)));

before() : callF() {
System.out.println("before call f()");
}
}

   function中的代码:

package com.gaojian.aspectj.test1;

public class Function {
	
	public void f() {
		System.out.println("method excution!");
	}
	
	public static void main(String[] args) {
		new Function().f();
	}
}

  Test中的代码:

package com.gaojian.aspectj.test1;

public class Test {
	public void foo(Function function) {
		function.f();
	}
	
	public void foo1(Function function){
		function.f();
	}
	public static void main(String[] args) {
		new Test().foo(new Function());
		System.out.println("*******differences**********");
		new Test().foo1(new Function());
	}
}

 执行test中的main函数运行结果为

before call f()
method excution!
*******differences**********
method excution!

 

程序分析:1:aj pointcut的execution(* f(..))匹配function中的f()函数;

               2:在类Test中调用f()函数应该都要执行aj中的前置通知函数;

               3:aj pointcut中有cflow(call(* com.gaojian.aspectj.test1.*.foo(..))):当执行连接点call(* com.gaojian.aspectj.test1.*.foo(..))匹配的函数Test中foo()函数的时候进行匹配。

               4:所以test中的foo执行时,aj中的前置函数执行了;则test中的foo1执行时,aj中的前置函数未执行了。

你可能感兴趣的:(使用)