AspectJ学习笔记
生活随笔
收集整理的這篇文章主要介紹了
AspectJ学习笔记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
介紹
切入點(diǎn)表達(dá)式
execution() 用于描述方法
<!--創(chuàng)建目標(biāo)類--> <bean id="userService" class="com.adolph.AOP.jdk.UserServiceImpl"></bean><!--創(chuàng)建切面類--> <bean id="myAspect" class="com.adolph.AOP.jdk.MyAspect"></bean><!--aop編程使用<aop:config>進(jìn)行配置proxy-target-class="true":使用cglib代理<aop:pointcut>切入點(diǎn),從目標(biāo)對象獲得具體方法<aop:advisor> 特殊的切面,只有一個(gè)通知和一個(gè)切入點(diǎn)advice-ref:通知引用pointcut-ref:切入點(diǎn)引用切入點(diǎn)表達(dá)式:execution(* com.adolph.AOP.jdk.*.*(..))選擇方法 返回值任意 包 類名任意 方法名任意 參數(shù)任意 --> <aop:config proxy-target-class="true"><aop:pointcut id="myPointCut" expression="execution(* com.adolph.AOP.jdk.*.*(..))"/><aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut"/> </aop:config> 復(fù)制代碼語法:execution(修飾符 返回值 包.類.方法(參數(shù)) throws 異常)
綜合
`execution(* com.adolph.AOP.jdk.*.*(..))` 復(fù)制代碼AspectJ通知類型
基于XML
目標(biāo)類:接口+實(shí)現(xiàn)
切面類:編寫多個(gè)通知,采用aspectJ通知名稱任意(方法名任意)
aop編程:將通知應(yīng)用到目標(biāo)類
測試
目標(biāo)類
public class UserServiceImpl {public void addUser() {System.out.println("addUser");}public void updateUser() {System.out.println("updateUser");}public void deleteUser() {System.out.println("deleteUser");} } 復(fù)制代碼切面類
/*** 切面類,含有多個(gè)通知*/ public class MyAspect {public void myBefore(JoinPoint joinPoint) {System.out.println("前置通知" + joinPoint.getSignature().getName()); //獲得目標(biāo)方法名}public void myAfterReturning(JoinPoint joinPoint, Object ret) {System.out.println("后置通知" + joinPoint.getSignature().getName() + "____" + ret); //獲得目標(biāo)方法名}public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("前");//手動執(zhí)行目標(biāo)方法Object proceed = joinPoint.proceed();System.out.println("后");return proceed;}public void MyAfterThrowing(JoinPoint joinPoint,Throwable e){System.out.println("拋出異常通知:");}public void after(JoinPoint joinPoint){System.out.println("最終");} } 復(fù)制代碼xml
<!--創(chuàng)建目標(biāo)類--> <bean id="userService" class="com.adolph.AOP.jdk.UserServiceImpl"></bean><!--創(chuàng)建切面類--> <bean id="myAspect" class="com.adolph.AOP.jdk.MyAspect"></bean> <!--aop編程 --> <aop:config><!--將切面類 聲明成切面,從而獲得通知(方法),ref:切面類引用--><aop:aspect ref="myAspect"><!--聲明一個(gè)切入點(diǎn),所有的通知都可以使用expression:切入點(diǎn)表達(dá)式id:名稱用于其他通知引用--><aop:pointcut id="myPointcut" expression="execution(* com.adolph.AOP.jdk.UserServiceImpl.*(..))"></aop:pointcut><!--前置通知method:通知,及方法名pointcut:切入點(diǎn)表達(dá)式,此表達(dá)式只能當(dāng)前通知使用pointcut-ref:切入點(diǎn)引用,可以與其他通知點(diǎn)共享切入點(diǎn)通知方法格式:public void myBefore(JoinPoint joinPoint)參數(shù)JoinPoint 用于描述連接點(diǎn)(目標(biāo)方法),獲得目標(biāo)方法名稱等--><aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before><!--后置通知,目標(biāo)方法執(zhí)行,獲得返回值public void myAfterReturning(JoinPoint joinPoint,Object ret)參數(shù)1:連接點(diǎn)描述參數(shù)2:類型Object,參數(shù)名returning配置的--><aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="ret"></aop:after-returning><!--環(huán)繞通知public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable返回值:Object方法名:任意參數(shù):Proceeding JoinPoint拋出異常--><aop:around method="myAround" pointcut-ref="myPointcut"></aop:around><!--異常通知throwing:通知方法的第二個(gè)參數(shù)名稱--><aop:after-throwing method="MyAfterThrowing" pointcut-ref="myPointcut" throwing="e"></aop:after-throwing><!--最終通知--><aop:after method="after" pointcut-ref="myPointcut"></aop:after></aop:aspect> </aop:config> 復(fù)制代碼基于注解
替換bean
<!--掃描--> <context:component-scan base-package="com.adolph.AOP"></context:component-scan> 復(fù)制代碼 public class MyAspect 復(fù)制代碼必須進(jìn)行aspectj自動代理
<!--確定aop注解生效--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 復(fù)制代碼聲明切面
public class MyAspect 復(fù)制代碼設(shè)置前置通知
("execution(* com.adolph.AOP.jdk.UserServiceImpl.*(..))")public void myBefore(JoinPoint joinPoint) {System.out.println("前置通知" + joinPoint.getSignature().getName()); //獲得目標(biāo)方法名} 復(fù)制代碼替換公共切入點(diǎn)
("execution(* com.adolph.AOP.jdk.UserServiceImpl.*(..))") private void myPointCut(){} 復(fù)制代碼設(shè)置后置通知
(value = "myPointCut()",returning = "ret") public void myAfterReturning(JoinPoint joinPoint, Object ret) {System.out.println("后置通知" + joinPoint.getSignature().getName() + "____" + ret); //獲得目標(biāo)方法名 } 復(fù)制代碼設(shè)置環(huán)繞通知
(value = "myPointCut()") public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("前");//手動執(zhí)行目標(biāo)方法Object proceed = joinPoint.proceed();System.out.println("后");return proceed; } 復(fù)制代碼設(shè)置拋出異常
(value = "myPointCut()",throwing = "e") public void MyAfterThrowing(JoinPoint joinPoint,Throwable e){System.out.println("拋出異常通知:"); } 復(fù)制代碼最終通知
(value = "myPointCut()",throwing = "e") public void MyAfterThrowing(JoinPoint joinPoint,Throwable e){System.out.println("拋出異常通知:"); } 復(fù)制代碼轉(zhuǎn)載于:https://juejin.im/post/5ca23972e51d453c0f7d8e93
總結(jié)
以上是生活随笔為你收集整理的AspectJ学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Lang.String
- 下一篇: NOI2019省选模拟赛 第三场