2017-08-17 1573 views
3

是否可以根據Spring AOP中的參數名獲取方法參數值?Spring AOP根據參數名得到方法參數值

MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature(); 

Method method = signature.getMethod(); 

method.getParameters().getName() 
// possible to get the paramater names 

此方法將獲取參數名稱,而不是值。

proceedingJoinPoint.getArgs() 

將返回不名

然後是有可能得到基於參數名的值值?

回答

2

當我不得不使用AOP來記錄函數參數和它們的值時,我搜索了相同的東西,但似乎沒有直接的方法來根據參數名稱獲取值。

但是我發現我們有什麼用method.getParameters().getName()proceedingJoinPoint.getArgs()返回的值總是同步的,也就是說,對於功能

public void foo(String a, String b) 

稱爲

foo("hello", "world"); 

method.getParameters().getName()返回[ 「一」,「 b「]和proceedingJoinPoint.getArgs()返回[」hello「,」world「]。所以你可以通過索引遍歷數組,並且對於每個索引i,第i個參數名稱將對應於第i個參數值。

我找不到此行爲的支持文檔,但嘿,這段代碼已經在生產服務器上運行了大約一年,它從來沒有產生錯誤的結果。儘管如果有人可以鏈接到此行爲的文檔,我會很高興。你甚至可以挖掘reflectiion的代碼來驗證這種行爲。