Spring AOP程式設計增強表示式

2021-10-04 11:40:33 字數 1277 閱讀 9200

1. spring中通過切入點表示式定義具體切入點

指示符作用bean

用於匹配指定包名下型別內的方法執行

within

用於匹配指定包名下型別內的方法執行

execution

用於進行細粒度方法匹配執行具體業務

@annotation

用於匹配指定註解修飾的方法執行

1.1 bean表示式應用於類級別,實現粗粒度的控制:

bean(「userserviceimpl」))

指定乙個類中所有方法

bean("*serviceimpl")

指定所有的字尾為serviceimpl的類

1.2 within表示式應用增強 ,應用於類級別

within(「aop.service.userserviceimpl」)

指定類,只能指定乙個類

within(「aop.service.*」)

只包括當前目錄下的類

within(「aop.service…*」)

指定當前目錄包含所有子目錄中的類

1.3 execution表示式應用於方法級別,細粒度的控制:

execution(void aop.service.userserviceimpl.adduser())

匹配方法

execution(void aop.service.personserviceimpl.adduser(string))

方法引數必須為字串

execution(* aop.service….(…))

萬能配置

1.4 @annotaion表示式應用於方法級別,實現細粒度的控制:

@annotation(com.jt.common.anno.requestlog))

指定乙個需要實現增強功能的方法

aop程式設計中有五種型別的通知:

前置通知 (@before) 方法執行之前執行

返回通知 (@afterreturning) 方法return之後執行

異常通知 (@afterthrowing) 方法出現異常之後執行

後置通知 (@after) : 又稱之為最終通知(finally)

環繞通知 (@around) :重點

@afterreturning和@after容易混淆,後置通知和異常通知不會同時出現

Spring AOP 引入增強

上篇部落格寫到了 spring aop 不管是前置增強,後置增強,引入增強都是對方法的增強,但是是否考慮過對類進行增強呢?偉大的 spring 做到了,只是換了一種說法 introduction 引入 首先我們來說一下引入增強的目的 動態的讓被增強的類實現乙個介面 下面就寫一下 吧 定義了乙個新介面...

Spring AOP 前置增強攔截不到

最近在用aop寫乙個在新增操作前統一配置建立人建立時間等基本資訊的功能,但是發現無論如何都攔截不到該有的請求 createbyhandler execution com.isoft.edu.api.serviceimpl.persist id beforeadd beforeadd pointcut...

Spring AOP中 Around增強處理

初次學習spring 的aop,就寫了乙個日誌切面,對控制層的接受和返回請求進行切面。aspect component public class logaspect before log public void dobefore after log public void doafter after...