日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

重用切入点表达式

發(fā)布時(shí)間:2024/4/13 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 重用切入点表达式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

重用切入點(diǎn)定義

在編寫 AspectJ 切面時(shí), 可以直接在通知注解中書寫切入點(diǎn)表達(dá)式. 但同一個(gè)切點(diǎn)表達(dá)式可能會(huì)在多個(gè)通知中重復(fù)出現(xiàn).

AspectJ 切面中, 可以通過 @Pointcut 注解將一個(gè)切入點(diǎn)聲明成簡(jiǎn)單的方法. 切入點(diǎn)的方法體通常是空的, 因?yàn)閷⑶腥朦c(diǎn)定義與應(yīng)用程序邏輯混在一起是不合理的.

切入點(diǎn)方法的訪問控制符同時(shí)也控制著這個(gè)切入點(diǎn)的可見性. 如果切入點(diǎn)要在多個(gè)切面中共用, 最好將它們集中在一個(gè)公共的類中. 在這種情況下, 它們必須被聲明為 public. 在引入這個(gè)切入點(diǎn)時(shí), 必須將類名也包括在內(nèi). 如果類沒有與這個(gè)切面放在同一個(gè)包中, 還必須包含包名.

其他通知可以通過方法名稱引入該切入點(diǎn).

重用切入點(diǎn)定義示例代碼

package com.learn.spring.aspectJ;import java.util.Arrays;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;/*** 日志切面*/ @Component //標(biāo)識(shí)為組件 @Aspect //標(biāo)識(shí)為切面 @Order(3) //指定切面的優(yōu)先級(jí). 值越小,優(yōu)先級(jí)越高. 標(biāo)注@Order的切面比不標(biāo)注@Order切面的優(yōu)先級(jí)高 public class LoggingAspect {/*** 通知: 前置 后置 返回 異常 環(huán)繞*//*** 重用切入點(diǎn)表達(dá)式*/@Pointcut("execution(* com.learn.spring.aspectJ.*.*(..))")public void suibian(){}/*** 前置通知: 在方法執(zhí)行之前執(zhí)行.* JoinPoint:連接點(diǎn)對(duì)象 包含了連接點(diǎn)相關(guān)的信息:方法名 方法的參數(shù)等....*///@Before("execution(public int com.learn.spring.aspectJ.ArithmeticCalculator.add(int,int))")//@Before("execution(public int com.learn.spring.aspectJ.ArithmeticCalculator.*(int,int))")//任意修飾符任意返回值 com.learn.spring.aspectJ包下的任意類的任意方法 任意參數(shù)//@Before("execution(* com.learn.spring.aspectJ.*.*(..))")@Before("suibian()")public void beforeMethod(JoinPoint joinPoint){//獲取方法名:String methodName = joinPoint.getSignature().getName();//獲取參數(shù)Object [] args = joinPoint.getArgs();System.out.println("The method "+methodName+" begins with "+ Arrays.asList(args));}} package com.learn.spring.aspectJ;import java.util.Arrays;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component;@Component @Aspect @Order(2) public class ValidatorAspect {//@Before("execution(* com.learn.spring.aspectJ.*.*(..))")@Before("com.learn.spring.aspectJ.LoggingAspect.suibian()")public void beforeMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();Object [] args = joinPoint.getArgs();System.out.println("Validator===>The method "+methodName+" begins with" + Arrays.asList(args));} }

?

總結(jié)

以上是生活随笔為你收集整理的重用切入点表达式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。