Spring基于常用AspectJ切點表達式使用介紹
execution (常用,方法級別的匹配)
語法:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)
- modifiers-pattern:方法的訪問符,如public、protected、default,不能匹配private方法
- ret-type-pattern:方法的返回值類型,例:java.util.List、java.lang.String(類的全限定名,或者支持的簡寫如String、Integer)
- declaring-type-pattern:方法所在類的全路徑名,例:life.cqq.controller.UserController
- name-pattern:方法名
- param-pattern:方法的參數類型,例:java.util.List、java.lang.String
- throws-pattern:方法拋出的異常類型,例:java.lang.RuntimeException、java.lang.Exception
(注:返回值類型 & 參數類型支持泛型判斷)
帶有?的位,非必寫,即可省略。所以,一個表達式至少由3部分組成。例:
// 匹配所有方法
execution(* *(..))
若該位省略,例modifiers-pattern,意味著匹配該位能匹配的全部方法:public、protected、default修飾的方法
上例子:
@Pointcut("execution(public java.util.List<java.lang.Integer> *.test(java.util.List<java.lang.String>) throws java.lang.RuntimeException)")匹配所有的方法名為test,方法格式為下方格式的方法。
public List<Iteger> test(List<String> list) throws RuntimeException
通配符
- “..” : 應用在declaring-type-pattern上時,意味著掃描子孫包。應用在param-pattern時意味著任意類型
- “*” :任意匹配符
- “+” :表示按照類型匹配,必須追加在declaring-type-pattern中的類名后,表示所有的該類型的類、繼承和擴展該類型的類
上例子:
// 1. 關于 .. 通配符
// 匹配子孫包下的所有類的所有方法
execution(* life.cqq..*.*(..))// 2. 關于 * 通配符
// 匹配所有類的所有方法
execution(* *(..))// 匹配子孫包下帶有指定前綴的包下的所有類的所有方法
execution(* life.cqq..prefix*.*.*(..))// 3. 關于 + 通配符
// 匹配子孫包下繼承或實現(xiàn)了指定類型的類 以及 該類型的本類(若該類型不為接口) 下的所有方法
execution(* life.cqq..type+.*(..))
within (常用,類級別的匹配) 語法:
within(declaring-type-pattern)
execution語法中的declaring-type-pattern部分,從通配符的例子中提取出可應用于within的內容:
// 1. 關于 .. 通配符
// 匹配子孫包下的所有類的所有方法
within(life.cqq..*)// 2. 關于 * 通配符
// 匹配所有類的所有方法
within(*)// 匹配子孫包下帶有指定前綴的包下的所有類的所有方法
within(life.cqq..prefix*.*)// 3. 關于 + 通配符
// 匹配子孫包下繼承或實現(xiàn)了指定類型的類 以及 該類型的本類(若該類型不為接口) 下的所有方法
within(life.cqq..type+)
@annotation (常用,方法級別的匹配)
語法:
@annotation(annotation-type-pattern)
與execution一樣針對于方法,匹配添加了指定注解的方法。
@within (常用,類級別的匹配)
語法:
@within(annotation-type-pattern)
與within一樣針對于類,匹配添加了指定注解的類。
邏輯運算符
- &&
- ||
- !
應用在多個Point上組成稍微復雜的匹配表達式
上例子:
@Component
class AopBean {
public void test(String str) {
System.out.println("String");
}
public void test(Integer integer) {
System.out.println("Integer");
}
}
@Pointcut("within(life.cqq.aop.logicsymbol.AopBean)")
public void point1() {}
@Pointcut("execution(public void test(java.lang.String))")
public void point2() {}
@Pointcut("execution(public void test(java.lang.Integer))")
public void point3() {}
// @Around("point1() && point2()") : 匹配參數為String類型的方法
// @Around("point1() && (point2() || point3())") : 匹配參數為Integer、String類型的方法
// @Around("point1() && !point2())") : 匹配參數為Integer類型的方法
以上僅為平時常用的內容,還有其他許多寫法,如:args、@args、target、@target等
一次實際應用
需求:
- 基于AOP + 自定義注解的方式實現(xiàn)三個系統(tǒng)的接口請求參數的記錄。
- 需實現(xiàn)就近原則:優(yōu)先判斷方法上日志注解,若無在判斷方法上的注解。類上添加日志注解時,意味著類的全部方法都需要打印請求參數。
@Pointcut("execution(* life.cqq..controller.*.*(..))")
private void log() {}
@Pointcut("@within(life.cqq.common.newlog.annotation.AppOpnLog) || @annotation(life.cqq.common.newlog.annotation.AppOpnLog)")
public void appOpnLog() {
}
@Pointcut("@within(life.cqq.common.newlog.annotation.EsOpnLog) || @annotation(life.cqq.common.newlog.annotation.EsOpnLog)")
public void esOpnLog() {
}
@Pointcut("@within(life.cqq.common.newlog.annotation.WebOpnLog) || @annotation(life.cqq.common.newlog.annotation.WebOpnLog)")
public void webOpnLog() {
}
@Around("appOpnLog() || esOpnLog() || webOpnLog()")
public Object opnLogAround(ProceedingJoinPoint point) throws Throwable {
// ......
return point.proceed();
}- 因為是三個系統(tǒng),log切點表達式中的controller前需要使用通配符"…"。因為每個系統(tǒng)的controller包名都是不一致的,但可以確定都符合格式: life.cqq.xxx.controller。
- 基于注解的形式,且實現(xiàn)就近原則,那么既要匹配類上的日志注解也要匹配方法上的日志注解,故使用@within 與 @annotation 并用邏輯或連接。具體是在方法還是在類上添加的注解,在Around方法中解析處理。
到此這篇關于Spring基于常用AspectJ切點表達式使用介紹的文章就介紹到這了,更多相關Spring AspectJ內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java Socket通信(一)之客戶端程序 發(fā)送和接收數據
對于Socket通信簡述,服務端往Socket的輸出流里面寫東西,客戶端就可以通過Socket的輸入流讀取對應的內容,Socket與Socket之間是雙向連通的,所以客戶端也可以往對應的Socket輸出流里面寫東西,然后服務端對應的Socket的輸入流就可以讀出對應的內容2016-03-03
SpringBoot整合SpringSecurity實現(xiàn)權限控制之實現(xiàn)多標簽頁
這篇文章主要介紹了SpringBoot整合SpringSecurity實現(xiàn)權限控制之實現(xiàn)多標簽頁,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11
JAVA 文件監(jiān)控 WatchService的示例方法
本篇文章主要介紹了JAVA 文件監(jiān)控 WatchService的示例方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
IDEA快速搭建spring?boot項目教程(Spring?initializr)
這篇文章主要介紹了IDEA快速搭建spring?boot項目教程(Spring?initializr),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

