AOP方法攔截獲取引數上的註解

2021-09-28 23:32:09 字數 1203 閱讀 4864

在spring aop中,無論是前置通知的引數joinpoint,還是環繞通知的引數proceedingjoinpoint,都可以通過以下方法獲得入參:

methodsignature signature=

(methodsignature) jp.

getsignature()

;

//根據原始碼分析,methodsignature封裝了兩個方法,乙個獲取方法的返回值型別,乙個是獲取封裝的method物件

public

inte***ce

methodsigernature

extends

condesigernature

getreturntype()

//可以用在環繞通知中,我們可以根據這個class型別,做定製化操作.

//而method的引數和引數上的註解,就可以從getmethod()返回的method物件中拿,api如下:

// 獲取方法上的註解

*** *** = signature.

getmethod()

.getannotation

(***.

class

)//獲取所有引數上的註解

annotation[

] parameterannotations= signature.

getmethod()

.getparameterannotations()

;

//獲取所有引數註解返回的是乙個二維陣列annotation,每個引數上可能有多個註解,是乙個一維陣列,

//多個引數又是一維陣列,就組成了二維陣列,所有我們在遍歷的時候,第一次遍歷拿到的陣列下標就是方法引數的下標,

for(annotation[

] parameterannotation: parameterannotations)

//再根據拿到所有的引數,根據指定的下標即可拿到物件的值

object[

] args= joinpoint.

getargs()

;for

(annotation[

] parameterannotation: parameterannotations)}}

//通過以上方法,即可找到你想要的引數註解,並拿到對應引數的值啦!

SpringAOP獲取方法引數上的註解

如下 自定義乙個引數註解 test,並將其使用到方法引數上,用於標註需要檢驗的引數 自定義註解,用於引數 target parameter documented retention retentionpolicy.runtime public inte ce test 介面層,使用使用 test註解...

aop切面 註解 引數獲取方法

在工作中會經常使用aop,這裡將aop使用基本方法,獲取在切點中使用的獲取引數 註解做乙個樣例。1 定義需要切面的註解 target elementtype.method retention retentionpolicy.runtime documented public inte ce annd...

aop阻止方法執行 Aop實現攔截方法引數

對於spring框架來說,最重要的兩大特性就是aop 和ioc。以前一直都知道有這兩個東西,在平時做的專案中也常常會涉及到這兩塊,像spring的事務管理什麼的,在看了些原始碼後,才知道原來事務管理也是用的aop來實現的。對於ioc的話,平時接觸的就更多了,什麼autowired,resource各...