spring aop 妙用 方法引數校驗

2021-09-01 02:32:39 字數 930 閱讀 2495

業務場景:在實現業務的過程中,往往都需要寫一些繁瑣的判斷引數是否為null的**,介面spring-aop 加註解實現通用的處理方法,使**更為簡潔

1.先寫好註解類

@retention(retentionpolicy.runtime)

@target()

@documented

public @inte***ce jparamscheck

* ocf =

* 測試git 提交

* **/

string ocf() default "";

}

上面這個註解,兩個引數,包含了兩種情況,還有很大的擴充套件空間,比如限制引數型別之類的!

2.實現切面

@aspect

@configuration

public class systemverifyaspect

string indexs = nci.split(",");

@suppresswarnings("rawtypes")

list lists = arrays.aslist(indexs);

boolean flag = true;

object obj = pjp.getargs();

forobj:for(int i=0;i上面我實現了環繞通知,切入點就是這個註解修飾的方法,,我們通過這個獲取這個註解的引數,然後通過規則來檢測判斷引數是否符合標準。這樣很輕鬆的就實現了對方法級別通用的引數校驗。

3.使用示例子

@jparamscheck(ocf="")

public responseobj typeupdate(customerfiletypeentity customerfiletype)

如上所示,我希望第乙個引數裡面的id不能為null

Spring Aop 修改目標方法引數和返回值

首先使用spring aop需要在spring的配置檔案中假如 一 新建註解 target retention retentionpolicy.runtime documented public inte ce handlefield二 建立切面 component changeidnoaophan...

SpringAOP獲取方法引數上的註解

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

SpringAOP處理通知中的引數

在spring中嘗試使用其他指示器時,會丟擲illegalargument exception異常。如上的這些指示器,只有exception指示器是實際執行匹配的,而其他都是用來限制匹配的。帶引數的切點表示式分解 在該切點表示式中使用了args tracknumber 限定符。表示傳遞給playtr...