淺談Spring AOP中args()和argNames的含義
1、切入點(diǎn)表達(dá)式部分如果增加了args()部分,那么目標(biāo)方法除了要滿足execution部分,還要滿足args()對方法參數(shù)的要求,對于符合execution表達(dá)式,但不符合args參數(shù)的方法,不會(huì)被植入切面。
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ù)傳入到增強(qiáng)處理方法after的參數(shù)中,id參數(shù)對應(yīng)userId,age參數(shù)對應(yīng)userAge。使用的方法是按順序一一對應(yīng),helloWorld第一個(gè)參數(shù)對args第一個(gè)參數(shù),helloWorld第2個(gè)參數(shù)對args第2個(gè)參數(shù)。
切入點(diǎn)表達(dá)式部分增加了&&args(userId, userAge)部分,意味著可以在增強(qiáng)處理方法中定義userId、userAge兩個(gè)形參------定義這兩個(gè)形參時(shí),形參類型可以隨意指定,但是一旦指定,譬如這里分別是String類型和Integer類型,這兩個(gè)形參類型將用于限制該切入點(diǎn)只匹配第一個(gè)參數(shù)類型為String,第二個(gè)參數(shù)類型為Integer的方法。
也就是,args()中的參數(shù)會(huì)和目標(biāo)方法的參數(shù)除了在順序上一一對應(yīng)之外,在類型上也要對應(yīng),否則匹配失敗,如下兩種情況都會(huì)匹配失敗。
@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á)式時(shí)還可使用如下形式:args(userId, userAge,..),這表明增強(qiáng)處理方法中可以通過userId, userAge來訪問目標(biāo)方法的參數(shù)。注意上面args表達(dá)式括號中的2點(diǎn),它表示可以匹配更多參數(shù),但是只要前兩個(gè)userId, userAge參數(shù)匹配上了,目標(biāo)方法就可以被匹配上。
argNames是可選的,如果沒有argNames這個(gè)參數(shù),而編譯器設(shè)置了【在class文件生成變量調(diào)試信息】,則spring可以通過反射知道方法參數(shù)的名字,通過名字配對,Spring知道args(userId, userAge)表達(dá)式里面的userId和userAge,對應(yīng)了增強(qiáng)方法public void after(JoinPoint point, String userId, Integer userAge)方法里面的userId和userAge,就是第一個(gè)示例的情況:
總結(jié):目標(biāo)方法和args()通過參數(shù)順序一一進(jìn)行匹配
args()和增強(qiáng)方法通過參數(shù)名稱一致進(jìn)行匹配。
但是,如果設(shè)置了argNames,Spring不再使用方法參數(shù)的名字來配對,使用argNames定義的順序來給
after(JoinPoint point, String userAge, String userId)的參數(shù)傳值,例如:argNames='userId,userAge',userId在userAge前面,表示after方法第一個(gè)參數(shù)(JoinPoint 除外)是userId,第二個(gè)參數(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注意:這一次兩個(gè)參數(shù)的類型都給成String類型了
總結(jié):目標(biāo)方法和args()通過參數(shù)順序一一進(jìn)行匹配
args()和argNames通過參數(shù)名稱一致進(jìn)行匹配
argNames和增強(qiáng)方法通過參數(shù)順序一一對應(yīng)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟2. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)3. css代碼優(yōu)化的12個(gè)技巧4. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算5. 教你如何寫出可維護(hù)的JS代碼6. IDEA版最新MyBatis程序配置教程詳解7. 使用Python和百度語音識別生成視頻字幕的實(shí)現(xiàn)8. idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析9. phpstudy apache開啟ssi使用詳解10. .NET SkiaSharp 生成二維碼驗(yàn)證碼及指定區(qū)域截取方法實(shí)現(xiàn)
