aspectj支持用 pointcut a捕获 pointcut b 的作用域或环境中的所有连接点。
每个pointcut在程序的控制流程中都有一个具体位置,这就提供了该pointcut的环境。
捕获通过初试连接点开始的程序控制流程内的所有连接点
通俗说,就是捕获一个pointcut的控制流中的所有连接点
cflow(Pointcut)
Picks out each join point in the control flow of any join point P picked out by Pointcut, including P itself.
这个图只要理解控制流程是什么就行,可以理解为pointcut内的整个程序调用顺序
demo
package aspectj;
public class MyClass {
public void doSth(){
System.out.println("do");
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.doSth();
}
}
package aspectj;
public aspect HelloWorld {
//捕获方法调用
pointcut callPoint(): call(void MyClass.doSth());
//捕获"方法调用"这个特定pointcut中控制流程中的所有连接点
pointcut cflowPoint(): cflow(callPoint());
before() : cflowPoint() && !within(HelloWorld){
System.out.println(thisJoinPoint.getSignature());
System.out.println(thisJoinPoint.getSourceLocation());
System.out.println(thisJoinPoint.getKind());
System.out.println();
}
}
输出
void aspectj.MyClass.doSth()
MyClass.java:10
method-call
void aspectj.MyClass.doSth()
MyClass.java:4
method-execution
PrintStream java.lang.System.out
MyClass.java:5
field-get
void java.io.PrintStream.println(String)
MyClass.java:5
method-call
do
捕获程序控制流程内的所有连接点,不包含初始连接点
上面说的是就是捕获一个pointcut的控制流中的所有连接点
这里不希望包括初始连接点,只要控制流中除去初始连接点之后的所有连接点
cflowbelow(Pointcut)
Picks out each join point in the control flow of any join point P picked out by Pointcut, but not P itself.
说明:
假设控制流的所有连接点顺序是
joinpoint a(初始连接点) -> joinPoint b -> joinPoint c;
那么cflow和cflow捕获的连接点顺序是
cflow:
joinpoint a(初始连接点) -> joinPoint b -> joinPoint c;
cflowbelow:
joinPoint b -> joinPoint c;
区别在于第一个连接点是否需要被包含
demo
package aspectj;
public aspect HelloWorld {
//捕获方法调用
pointcut callPoint(): call(void MyClass.doSth());
//捕获"方法调用"这个特定pointcut中控制流程中
//除去初始joinPoint以外的所有连接点
pointcut cflowbelowPoint(): cflowbelow(callPoint());
before() : cflowbelow() && !within(HelloWorld){
System.out.println(thisJoinPoint.getSignature());
System.out.println(thisJoinPoint.getSourceLocation());
System.out.println(thisJoinPoint.getKind());
System.out.println();
}
}
输出
void aspectj.MyClass.doSth()
MyClass.java:4
method-execution
PrintStream java.lang.System.out
MyClass.java:5
field-get
void java.io.PrintStream.println(String)
MyClass.java:5
method-call
do
和上面对比,发现即少了第一个初始连接点,method call
思考
cflow和cflowbelow的区别
refer
https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
http://blog.csdn.net/zl3450341/article/details/7673967
《aspectj cookbook》