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