spring中的aspectJ表達式

Spring AOP supports the following AspectJ pointcut designators (PCD) for use in pointcut expressions:

 

表 達 式

說明

execution

匹配方法執行連接點。我們可以指定包、類或者方法名,以及方法的可見性、返回值和參數類型。這是應用的最爲廣泛的切入點表達式。例如,execution(* com.apress..TestBean.*(..)表示執行com.apress包中TestBean中的所有方法,無論是何種參數或返回類型

within

匹配那些在已聲明的類型中執行的連接點。例如,within(com.apress..TestBean)匹配從TestBean的方法中產生的調用

this

通過用bean引用(即AOP代理)的類型跟指定的類型作比較來匹配連接點。例如,this(SimpleBean)將只會匹配在SimpleBean類型的bean上的調用

target

通過用被調用的bean的類型和指定的類型作比較來匹配連接點。比如target(SimpleBean)將只會匹配在SimpleBean類型bean上方法的調用

args

通過比較方法的參數類型跟指定的參數類型來匹配連接點。例如,args(String, String)將只會匹配那些有兩個String類型參數的方法

@target

通過檢查調用的目標對象是否具有特定註解來匹配連接點。例如,@target(Magic)將會匹配帶有@Magic註解的類中的方法調用

@args

跟args相似,不過@args檢查的是方法參數的註解而不是它們的類型。例如,@args(NotNull)會匹配所有那些包含一個被標註了@NotNull註解的參數的方法

@within

跟within相似,這個表達式匹配那些帶有特定註解的類中執行的連接點。例如,表達式@within(Magic)將會匹配對帶有@Magic註解的類型的bean上方法的調用

@annotation

通過檢查將被調用的方法上的註解是否爲指定的註解來匹配連接點。例如,@annotation(Magic)將會匹配所有標有@Magic註解的方法調用

bean

通過比較bean的ID(或名稱)來匹配連接點。我們也可以在bean名模式中使用通配符。例如,bean("simple")將會匹配ID或名稱爲simple的bean中的連接點

 

Other pointcut types

The full AspectJ pointcut language supports additional pointcut designators that are not supported in Spring. These are: call, get, set, preinitialization, staticinitialization, initialization, handler, adviceexecution, withincode, cflow, cflowbelow, if, @this, and @withincode. Use of these pointcut designators in pointcut expressions interpreted by Spring AOP will result in an IllegalArgumentException being thrown.

The set of pointcut designators supported by Spring AOP may be extended in future releases to support more of the AspectJ pointcut designators.

 

* 匹配任意字符,只能匹配一個元素

..  匹配任意字符 匹配多個元素,在類的路徑中匹配任意數量的子包,表示類時要和*聯合使用, 在方法中匹配任意數量的參數

+ 匹配任意類型的子類型,作爲後綴用在類後

execution() 和 within() 支持所有通配符

args() this() target()只支持+

@args() @within() @target @annotation不支持通配符

可以使用 '&&', '||' and '!' 在xml中&&需要轉義,可以用and代替

 

 

java.lang.String 匹配String類型

 

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)   throws-pattern?)

 

未完待續

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章