Spring AOP註解和切面表示式詳解

2021-09-16 21:34:04 字數 3883 閱讀 4329

集中處理某一關注點/橫切邏輯

可以很方便的新增/刪除關注點

侵入性少,增強**可讀性以及可維護性

許可權控制;

快取控制;

事務控制;

審計日誌;

效能監控;

分布式追蹤;

異常處理;

切面表示式主要由三部分組成:

designators(指示器):execution()等;

wildcards(萬用字元): * … +

operators(操作符):&& || !

wildcards(萬用字元)

operators(操作符)

designators(指示器)

匹配方法

execution(《修飾符》? 《返回型別》 《包名》?《方法名》(《引數》)異常?)

execution(

modifier-pattern?

returntype-pattern

package-pattern?

methodname-pattern(args-pattern)

throwexception-pattern?

)execution(*[修飾符]? *[返回值] *[包路徑]?*[方法名](..[引數])throw *[異常類]?)

修飾符匹配(modifier-pattern)

//匹配所有public修飾的方法

@pointcut("execution(public * **(..))")

public void extest1(){}

返回值匹配(returntype-pattern)

//匹配所有string或者void返回值的方法

@pointcut("execution(string||void **(..))")

public void extest2(){}

包路徑匹配(package-pattern)

//com包下的所有類的方法

@pointcut("execution(* com.*.*(..))")

public void extest3(){}

//如果不使用..匹配到了類級別的名字,需要類.方法名.............

//com包下的所有子包的所有類的方法

@pointcut("execution(* com..*(..))")

public void extest4(){}

//com包下的所有子包的productservice類的方法

@pointcut("execution(* com..productservice.*(..))")

public void extest5(){}

方法名匹配(methodname-pattern)

//匹配所有test名字開頭的方法

@pointcut("execution(* test*(..))")

public void extest6(){}

//匹配所有包含test名字的方法

@pointcut("execution(* *test*(..))")

public void extest7(){}

引數匹配(args-pattern)

//匹配所有引數列表的方法

@pointcut("execution(* *(..))")

public void extest8(){}

//匹配無引數列表的方法

@pointcut("execution(* *())")

public void extest9(){}

異常匹配(throw***ception-pattern)

//匹配所有拋過異常的方法

@pointcut("execution(* *()throws *)")

public void extest10(){}

//只匹配所有丟擲空指標異常的方法

@pointcut("execution(* *()throws nullpointerexception)")

public void extest11(){}

匹配註解
//匹配所有使用了aspectannotation註解的類的所有方法(要求註解的retentionpolicy的級別為runtime)

@pointcut("@target(com.tiglle.manage.aspectannotation)")

public void targetmatch(){}

//匹配所有使用了aspectannotation註解為引數的方法

@pointcut("@args(com.tiglle.manage.aspectannotation)")

public void argsmatch(){}

//匹配所有使用了aspectannotation註解的類的所有方法(要求註解的retentionpolicy的級別為class)

@pointcut("@within(com.tiglle.manage.aspectannotation)")

public void withinmatch(){}

//方法註解匹配,匹配所有帶aspectannotation註解的方法

@pointcut("@annotation(com.tiglle.manage.annotation.aspectannotation)")

public void test()

匹配包或者型別

如果傳的是全類名(包名.類名):匹配此類下所有的方法

//匹配testservice類中的所有方法

@pointcut("within(com.tiglle.service.testservice)")

public voud test(){}

如果傳的時包名:匹配此包下所有類的方法

//匹配com/tiglle/包下所有包和子包中的類中的所有方法

@pointcut("within(com.tiglle..*)")

public voud test(){}

匹配物件
//匹配**物件和普通物件及其所有子類的方法

@pointcut("this(com.tiglle.manage.service.productservice)")

public void thismatch()

//根據spring容器的bean的名稱(id)匹配,(不匹配子類)

@pointcut("bean(productservice)")

public void beanmatch()

//匹配目標物件和普通物件及其所有子類的方法

@pointcut("target(com.tiglle.manage.service.productservice)")

public void targetmatch()

匹配引數
//匹配spring容器所有此引數型別和列表的方法(string,long)

@pointcut("args(string,long)")

public void argsmatch(){}

//匹配spring容器所有此引數型別和列表的方法(第乙個為long,後面隨意)

@pointcut("args(long,..)")

public void argsmatch2(){}

springAOP註解式切面實現

org.springframework spring aop 4.3.11.release org.springframework spring aspects 4.3.11.release 我這裡監聽的是service層 component aspect public class testaspe...

Spring Aop 切面的應用 註解

本次記錄的是使用spring註解的方式來實現切面程式設計.實現環境 設定乙個分布式鎖,在修改資料的時候,判斷鎖,加鎖,完成資料修改後,釋放鎖.自定義乙個鎖註解,新增到方法上,在執行方法前根據引數設定鎖key,target retention retentionpolicy.runtime publi...

spring AOP 多個切面

切面 日誌 author w7 public class logger 切面 安全框架 author w7 public class security 切面 許可權 author w7 public class privilege public void setaccess string acces...