spring 切面程式設計

2021-08-13 22:06:29 字數 1485 閱讀 2138

spring aop就是乙個同心圓,要執行的方法為圓心,最外層的order最小。從最外層按照aop1、aop2的順序依次執行doaround方法,dobefore方法。然後執行method方法,最後按照aop2、aop1的順序依次執行doafter、doafterreturn方法。也就是說對多個aop來說,先before的,一定後after。

如果我們要在同乙個方法事務提交後執行自己的aop,那麼把事務的aop order設定為2,自己的aop order設定為1,然後在doafterreturn裡邊處理自己的業務邏輯。

表示式結構解釋如下:

1.通過方法修飾符定義切點 

匹配所有的public修飾符的方法:

execution(public * *(..))

2.通過方法名定義切點

匹配所有」set」開頭的方法:

execution(* set*(..))

3.通過類定義切點

匹配accountservice 介面的所有方法:

execution(* com.xyz.service.accountservice.*(..))

4.通過包定義切點

匹配service包中的所有方法:

execution(* com.xyz.service..(..))

匹配service包及其子包中的所有方法:

execution(* com.xyz.service…(..))

/**

* created by jiyang on 10:30 2017/12/22

*/@component

@aspect

@slf4j

@profile("dev")

@order(2)

public

class

fortestaspect1

// 獲取所有註解

annotation annotations = method.getparameterannotations();

for (int i = 0; i < annotations.length; i++)

}if (a.annotationtype().equals(pathvariable.class)) }}}}}

/**

* created by jiyang on 12:39 2017/12/22

*/@component

@aspect

@profile("dev")

@slf4j

@order(1)

public

class

fortestaspect2 catch (throwable e) finally }}

Spring面向切面程式設計

1 面向切面程式設計 aop 的概念 把專案中需要在多處用到的功能,比如日誌 安全和事物等集中到乙個類中處理,而不用在每個需要用到該功能的地方顯式呼叫。2 術語解釋 橫切關注點 分布應用於多處的功能 切面 橫切關注點可以被模組化為乙個類,這個類被稱為乙個切面 通知 advice 切面要完成的工作。s...

Spring面向切面程式設計AOP(around)

詳細分析spring aop實現的jdk aop,與cglib實現aop 1 採用注釋注入的方式 配置檔案 表示使用cglib動態 技術織入增強 編寫 import org.aspectj.lang.proceedingjoinpoint import org.aspectj.lang.annota...

Spring面向切面程式設計AOP

感謝zejian 大佬的分享 關於 spring aop aspectj 你該知曉的一切 大佬的分享讓我受益匪淺!首先學習aop前,弄清楚為什麼要使用aop?舉個栗子有助於理解 乙個支付轉賬的小栗子 我們每次使用手機支付時,不管轉賬還是支付都需要驗證支付資訊密碼。這時aop的好處就體現出來了,我們可...