跟我學aspectj之四 ----- pointcut基礎語法
阿新 • • 發佈:2019-02-14
一、aspect的定義
執行完HelloWorld以後,我們來看下aspect的基礎語法:
1、定義一個切面: 關鍵字aspect。 這定義Java類的語法類似。
2、定義pointcut: [修飾符(public,protected.....)] pointcut poincut名字() : 表示式;
3、定義advice: 通知型別() : pointcut名字(){ .......邏輯}
一個最基本的aspect,就是這樣組成的。值得一提的是:aspectj支援很多型別的pointcut,最基本的就是method call pointcut(方法級別),而Spring的aop 僅支援method call pointcut。所以,在後面陸續的使用中,你將會發現aspectj的強大, 簡直強大到有點過分。而至於advice,aspectj也一樣,就是5種類型。
二、pointcut的主要型別
Methods and Constructors | |
call(Signature) | every call to any method or constructor matching Signature at the call site(方法和建構函式的呼叫點) |
execution(Signature) | every execution of any method or constructor matching Signature (方法和建構函式的執行點) |
Fields | |
get(Signature) | every reference to any field matching Signature (屬性的讀操作) |
set(Signature) | every assignment to any field matching Signature. The assigned value can be exposed with anargs pointcut (屬性的寫操作) |
Exception Handlers | |
handler(TypePattern) | every exception handler for any Throwable type in TypePattern. The exception value can be exposed with anargs pointcut(異常處理執行) |
Advice |
|
adviceexecution() | every execution of any piece of advice(Advice執行) |
Initialization | |
staticinitialization(TypePattern) | every execution of a static initializer for any type in TypePattern (類初始化) |
initialization(Signature) | every initialization of an object when the first constructor called in the type matchesSignature, encompassing the return from the super constructor call to the return of the first-called constructor (物件初始化) |
preinitialization(Signature) | every pre-initialization of an object when the first constructor called in the type matchesSignature, encompassing the entry of the first-called constructor to the call to the super constructor (物件預先初始化) |
Lexical | |
within(TypePattern) | every join point from code defined in a type in TypePattern (捕獲在指定類或者方面中的程式體中的所有連線點,包括內部類) |
withincode(Signature) | every join point from code defined in a method or constructor matching Signature (用於捕獲在構造器或者方法中的所有連線點,包括在其中的本地類) |
Instanceof checks and context exposure | |
this(Type or Id) | every join point when the currently executing object is an instance of Type orId's type(所有Type or id 的例項的執行點,匹配所有的連線點,如方法呼叫,屬性設定,當前的執行物件為Account,或者其子類。) |
target(Type or Id) | every join point when the target executing object is an instance of Type orId's type (配所有的連線點,目標物件為Type或Id) |
args(Type or Id, ...) | every join point when the arguments are instances of Types or the types of theIds (引數型別為Type) |
Control Flow | |
cflow(Pointcut) | every join point in the control flow of each join point P picked out byPointcut, including P itself (捕獲所有的連線點在指定的方法執行中,包括執行方法本身) |
cflowbelow(Pointcut) | every join point below the control flow of each join point P picked out byPointcut; does not include P itself (捕獲所有的連線點在指定的方法執行中,除了執行方法本身) |
Conditional | |
if(Expression) | every join point when the boolean Expression is true |
Combination (邏輯/結合操作) | |
! Pointcut | every join point not picked out by Pointcut |
Pointcut0 && Pointcut1 | each join point picked out by both Pointcut0 and Pointcut1 |
Pointcut0 || Pointcut1 | each join point picked out by either Pointcut0 or Pointcut1 |
( Pointcut ) | each join point picked out by Pointcut |
前面說過pointcut基於正則的語法,那麼肯定也支援萬用字元,含義如下:
* 表示任何數量的字元,除了(.)
.. 表示任何數量的字元包括任何數量的(.)
+ 描述指定型別的任何子類或者子介面
同java一樣,提供了一元和二元的條件表達操作符。
一元操作符:!
二元操作符:||和&&
優先權同java