成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

淺談Spring AOP中args()和argNames的含義

瀏覽:2日期:2023-07-04 08:42:27
args()的作用主要有兩點:

1、切入點表達(dá)式部分如果增加了args()部分,那么目標(biāo)方法除了要滿足execution部分,還要滿足args()對方法參數(shù)的要求,對于符合execution表達(dá)式,但不符合args參數(shù)的方法,不會被植入切面。

2、定義了args()之后,才能把目標(biāo)方法的參數(shù)傳入到切面方法的參數(shù)中(通過Joinpoint也可以獲取參數(shù),但當(dāng)前方法是直接用切面方法參數(shù)接受)。

示例1

目標(biāo)方法:

@RestController@RequestMapping('/testAop')public class TestController { private Logger logger = LoggerFactory.getLogger(TestController.class); @RequestMapping('/helloworld') public String helloWorld(String id, Integer age){System.out.println('被代理方法正在執(zhí)行');return null; }}

切面方法

@After('execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)') public void after(JoinPoint point, String userId, Integer userAge){System.out.println('userId===========' + userId);System.out.println('userAge===========' + userAge); }

輸出結(jié)果:

被代理方法正在執(zhí)行userId===========bian1996userAge===========24

定義了args(userId, userAge)才能把目標(biāo)方法helloWorld(String id, Integer age)的參數(shù)傳入到增強處理方法after的參數(shù)中,id參數(shù)對應(yīng)userId,age參數(shù)對應(yīng)userAge。使用的方法是按順序一一對應(yīng),helloWorld第一個參數(shù)對args第一個參數(shù),helloWorld第2個參數(shù)對args第2個參數(shù)。

切入點表達(dá)式部分增加了&&args(userId, userAge)部分,意味著可以在增強處理方法中定義userId、userAge兩個形參------定義這兩個形參時,形參類型可以隨意指定,但是一旦指定,譬如這里分別是String類型和Integer類型,這兩個形參類型將用于限制該切入點只匹配第一個參數(shù)類型為String,第二個參數(shù)類型為Integer的方法。

也就是,args()中的參數(shù)會和目標(biāo)方法的參數(shù)除了在順序上一一對應(yīng)之外,在類型上也要對應(yīng),否則匹配失敗,如下兩種情況都會匹配失敗。

@RequestMapping('/helloworld')public String helloWorld(Integer id, Integer age){ System.out.println('被代理方法正在執(zhí)行'); return null;} @After('execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)')public void after(JoinPoint point, String userId, String userAge){ System.out.println('userId===========' + userId); System.out.println('userAge===========' + userAge);}@RequestMapping('/helloworld')public String helloWorld(Integer sex, String id, Integer age){ System.out.println('被代理方法正在執(zhí)行'); return null;} @After('execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)') public void after(JoinPoint point, String userId, Integer userAge){ System.out.println('userId===========' + userId); System.out.println('userAge===========' + userAge); }

除此之外,使用args()表達(dá)式時還可使用如下形式:args(userId, userAge,..),這表明增強處理方法中可以通過userId, userAge來訪問目標(biāo)方法的參數(shù)。注意上面args表達(dá)式括號中的2點,它表示可以匹配更多參數(shù),但是只要前兩個userId, userAge參數(shù)匹配上了,目標(biāo)方法就可以被匹配上。

argNames是可選的,如果沒有argNames這個參數(shù),而編譯器設(shè)置了【在class文件生成變量調(diào)試信息】,則spring可以通過反射知道方法參數(shù)的名字,通過名字配對,Spring知道args(userId, userAge)表達(dá)式里面的userId和userAge,對應(yīng)了增強方法public void after(JoinPoint point, String userId, Integer userAge)方法里面的userId和userAge,就是第一個示例的情況:

總結(jié):

目標(biāo)方法和args()通過參數(shù)順序一一進(jìn)行匹配

args()和增強方法通過參數(shù)名稱一致進(jìn)行匹配。

但是,如果設(shè)置了argNames,Spring不再使用方法參數(shù)的名字來配對,使用argNames定義的順序來給

after(JoinPoint point, String userAge, String userId)的參數(shù)傳值,例如:argNames='userId,userAge',userId在userAge前面,表示after方法第一個參數(shù)(JoinPoint 除外)是userId,第二個參數(shù)是userAge,示例如下:

目標(biāo)方法

@RequestMapping('/helloworld')public String helloWorld(String id, String age){ System.out.println('被代理方法正在執(zhí)行'); return null;}

切面方法

@After(value = 'execution(* com.bxp.controller.TestController.*(..)) && args(userId, userAge)', argNames = 'userId,userAge')public void after(JoinPoint point, String userAge, String userId){ System.out.println('userId===========' + userId); System.out.println('userAge===========' + userAge);}

請求連接和輸出結(jié)果

請求連接http://localhost:8088/testAop/helloworld?age=24&id=bian1996輸出結(jié)果被代理方法正在執(zhí)行userId===========24userAge===========bian1996注意:這一次兩個參數(shù)的類型都給成String類型了

總結(jié):

目標(biāo)方法和args()通過參數(shù)順序一一進(jìn)行匹配

args()和argNames通過參數(shù)名稱一致進(jìn)行匹配

argNames和增強方法通過參數(shù)順序一一對應(yīng)。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章: