pointcut支持布爾表達(dá)式 &&,||,! 以及 匿名,重用
捕獲何時(shí)連接點(diǎn)上的運(yùn)行時(shí)條件為true
if(BooleanExpression)
Picks out each join point where the boolean expression evaluates to true. The boolean expression used can only access static members, parameters exposed by the enclosing pointcut or advice, and thisJoinPoint forms. In particular, it cannot call non-static methods on the aspect or use return values or exceptions exposed by after advice.
切入點(diǎn)結(jié)合復(fù)合表達(dá)式
即&& || !
結(jié)合上面的if demo如下
package aspectj;
public aspect HelloWorld {
//可以定義變量
private static int value = 0;
//!withincode 用來(lái)去除MyClass.get()里面的所有joinPoint
//!execution 用來(lái)去除MyClass.get() 執(zhí)行時(shí)的這個(gè)joinPoint
pointcut ifPoint(): if((thisJoinPoint.getThis() instanceof MyClass) &&
((MyClass)thisJoinPoint.getThis()).get() > value) &&
!withincode(* MyClass.get()) &&
!execution(* MyClass.get());
after() : ifPoint() && !within(HelloWorld){
System.out.println(thisJoinPoint.getSignature());
System.out.println(thisJoinPoint.getSourceLocation());
System.out.println(thisJoinPoint.getKind());
System.out.println();
}
}
package aspectj;
public class MyClass {
public void doSth(int x){
}
public int get() {
return 3;
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.doSth(5);
}
}
輸出
aspectj.MyClass()
MyClass.java:3
constructor-execution
aspectj.MyClass()
MyClass.java:3
initialization
void aspectj.MyClass.doSth(int)
MyClass.java:4
method-execution
聲明匿名切入點(diǎn)
每一個(gè)切入點(diǎn)的聲明都用到了匿名切入點(diǎn)涂乌,也可以直接用在通知聲明上
比如上例中
if((thisJoinPoint.getThis() instanceof MyClass) &&((MyClass)thisJoinPoint.getThis()).get() > value)
!withincode(* MyClass.get())
!execution(* MyClass.get());
!within(HelloWorld)
這里面每一個(gè)都可叫匿名切入點(diǎn)
重用切入點(diǎn)
就是聲明了切入點(diǎn)之后,在其他地方引用該pointcut
比如上例中
pointcut ifPoint():xxxx(省略) 這里是聲明切入點(diǎn)
after() : ifPoint() && !within(HelloWorld) 這里重用了切入點(diǎn) ifPoint()
思考
demo中withincode和execution的區(qū)別
execution只是一個(gè)連接點(diǎn),即函數(shù)執(zhí)行的那一個(gè)
withincode則是函數(shù)執(zhí)行里面所有連接點(diǎn)狸吞,比如調(diào)用其他方法叫倍,獲取field
refer
https://eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
《aspectj cookbook》