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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AOP举例子

發布時間:2025/4/9 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AOP举例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

切面類TestAspect

Java代碼 ?
  • package?com.spring.aop;??
  • /**?
  • ?*?切面?
  • ?*?
  • ?*/??
  • public?class?TestAspect?{??
  • ??
  • ????public?void?doAfter(JoinPoint?jp)?{??
  • ????????System.out.println("log?Ending?method:?"??
  • ????????????????+?jp.getTarget().getClass().getName()?+?"."??
  • ????????????????+?jp.getSignature().getName());??
  • ????}??
  • ??
  • ????public?Object?doAround(ProceedingJoinPoint?pjp)?throws?Throwable?{??
  • ????????long?time?=?System.currentTimeMillis();??
  • ????????Object?retVal?=?pjp.proceed();??
  • ????????time?=?System.currentTimeMillis()?-?time;??
  • ????????System.out.println("process?time:?"?+?time?+?"?ms");??
  • ????????return?retVal;??
  • ????}??
  • ??
  • ????public?void?doBefore(JoinPoint?jp)?{??
  • ????????System.out.println("log?Begining?method:?"??
  • ????????????????+?jp.getTarget().getClass().getName()?+?"."??
  • ????????????????+?jp.getSignature().getName());??
  • ????}??
  • ??
  • ????public?void?doThrowing(JoinPoint?jp,?Throwable?ex)?{??
  • ????????System.out.println("method?"?+?jp.getTarget().getClass().getName()??
  • ????????????????+?"."?+?jp.getSignature().getName()?+?"?throw?exception");??
  • ????????System.out.println(ex.getMessage());??
  • ????}??
  • ??
  • ????private?void?sendEx(String?ex)?{??
  • ????????//TODO?發送短信或郵件提醒??
  • ????}??
  • }???
  • ?

    ?

    Java代碼 ?
  • package?com.spring.service;??
  • /**?
  • ?*?接口A?
  • ?*/??
  • public?interface?AService?{??
  • ??????
  • ????public?void?fooA(String?_msg);??
  • ??
  • ????public?void?barA();??
  • }??
  • ?

    Java代碼 ?
  • package?com.spring.service;??
  • /**?
  • ?*接口A的實現類?
  • ?*/??
  • public?class?AServiceImpl?implements?AService?{??
  • ??
  • ????public?void?barA()?{??
  • ????????System.out.println("AServiceImpl.barA()");??
  • ????}??
  • ??
  • ????public?void?fooA(String?_msg)?{??
  • ????????System.out.println("AServiceImpl.fooA(msg:"+_msg+")");??
  • ????}??
  • }??
  • ?

    ?

    Java代碼 ?
  • package?com.spring.service;??
  • ??
  • /**?
  • ?*???Service類B?
  • ?*/??
  • public?class?BServiceImpl?{??
  • ??
  • ????public?void?barB(String?_msg,?int?_type)?{??
  • ????????System.out.println("BServiceImpl.barB(msg:"+_msg+"?type:"+_type+")");??
  • ????????if(_type?==?1)??
  • ????????????throw?new?IllegalArgumentException("測試異常");??
  • ????}??
  • ??
  • ????public?void?fooB()?{??
  • ????????System.out.println("BServiceImpl.fooB()");??
  • ????}??
  • ??
  • }??
  • ?

    ??? ApplicationContext

    Java代碼 ?
  • <?xml?version="1.0"?encoding="UTF-8"?>??
  • <beans?xmlns="http://www.springframework.org/schema/beans"??
  • ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
  • ????xmlns:aop="http://www.springframework.org/schema/aop"??
  • ????xsi:schemaLocation="??
  • ????????????http://www.springframework.org/schema/beans??
  • ????????????http://www.springframework.org/schema/beans/spring-beans-2.0.xsd??
  • ????????????http://www.springframework.org/schema/aop??
  • ????????????http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"??
  • ????default-autowire="autodetect">??
  • ????<aop:config>??
  • ????????<aop:aspect?id="TestAspect"?ref="aspectBean">??
  • ????????????<!--配置com.spring.service包下所有類或接口的所有方法-->??
  • ????????????<aop:pointcut?id="businessService"??
  • ????????????????expression="execution(*?com.spring.service.*.*(..))"?/>??
  • ????????????<aop:before?pointcut-ref="businessService"?method="doBefore"/>??
  • ????????????<aop:after?pointcut-ref="businessService"?method="doAfter"/>??
  • ????????????<aop:around?pointcut-ref="businessService"?method="doAround"/>??
  • ????????????<aop:after-throwing?pointcut-ref="businessService"?method="doThrowing"?throwing="ex"/>??
  • ????????</aop:aspect>??
  • ????</aop:config>??
  • ??????
  • ????<bean?id="aspectBean"?class="com.spring.aop.TestAspect"?/>??
  • ????<bean?id="aService"?class="com.spring.service.AServiceImpl"></bean>??
  • ????<bean?id="bService"?class="com.spring.service.BServiceImpl"></bean>??
  • ??
  • </beans>??
  • ?

    ??? 測試類AOPTest

    Java代碼 ?
  • public?class?AOPTest?extends?AbstractDependencyInjectionSpringContextTests?{??
  • ??????
  • ????private?AService?aService;??
  • ??????
  • ????private?BServiceImpl?bService;??
  • ??????
  • ????protected?String[]?getConfigLocations()?{??
  • ????????String[]?configs?=?new?String[]?{?"/applicationContext.xml"};??
  • ????????return?configs;??
  • ????}??
  • ??????
  • ??????
  • ????/**?
  • ?????*?測試正常調用?
  • ?????*/??
  • ????public?void?testCall()??
  • ????{??
  • ????????System.out.println("SpringTest?JUnit?test");??
  • ????????aService.fooA("JUnit?test?fooA");??
  • ????????aService.barA();??
  • ????????bService.fooB();??
  • ????????bService.barB("JUnit?test?barB",0);??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?測試After-Throwing?
  • ?????*/??
  • ????public?void?testThrow()??
  • ????{??
  • ????????try?{??
  • ????????????bService.barB("JUnit?call?barB",1);??
  • ????????}?catch?(IllegalArgumentException?e)?{??
  • ??????????????
  • ????????}??
  • ????}??
  • ??????
  • ????public?void?setAService(AService?service)?{??
  • ????????aService?=?service;??
  • ????}??
  • ??????
  • ????public?void?setBService(BServiceImpl?service)?{??
  • ????????bService?=?service;??
  • ????}??
  • }??
  • ?

    ??? 運行結果如下:

    Java代碼 ?
  • log?Begining?method:?com.spring.service.AServiceImpl.fooA??
  • AServiceImpl.fooA(msg:JUnit?test?fooA)??
  • log?Ending?method:?com.spring.service.AServiceImpl.fooA??
  • process?time:?0?ms??
  • log?Begining?method:?com.spring.service.AServiceImpl.barA??
  • AServiceImpl.barA()??
  • log?Ending?method:?com.spring.service.AServiceImpl.barA??
  • process?time:?0?ms??
  • log?Begining?method:?com.spring.service.BServiceImpl.fooB??
  • BServiceImpl.fooB()??
  • log?Ending?method:?com.spring.service.BServiceImpl.fooB??
  • process?time:?0?ms??
  • log?Begining?method:?com.spring.service.BServiceImpl.barB??
  • BServiceImpl.barB(msg:JUnit?test?barB?type:0)??
  • log?Ending?method:?com.spring.service.BServiceImpl.barB??
  • process?time:?0?ms??
  • ??
  • log?Begining?method:?com.spring.service.BServiceImpl.barB??
  • BServiceImpl.barB(msg:JUnit?call?barB?type:1)??
  • log?Ending?method:?com.spring.service.BServiceImpl.barB??
  • method?com.spring.service.BServiceImpl.barB?throw?exception??
  • 測試異常??
  • ?

    ??? 《Spring參考手冊》中定義了以下幾個AOP的重要概念,結合以上代碼分析如下:

    • 切面(Aspect) :官方的抽象定義為“一個關注點的模塊化,這個關注點可能會橫切多個對象”,在本例中,“切面”就是類TestAspect所關注的具體行為,例 如,AServiceImpl.barA()的調用就是切面TestAspect所關注的行為之一?!扒忻妗痹贏pplicationContext 中<aop:aspect>來配置。
    • 連接點(Joinpoint) :程序執行過程中的某一行為,例如,AServiceImpl.barA()的調用或者BServiceImpl.barB(String _msg, int _type)拋出異常等行為。
    • 通知(Advice) :“切面”對于某個“連接點”所產生的動作,例如,TestAspect中對com.spring.service包下所有類的方法進行日志記錄的動作就是一個Advice。其中,一個“切面”可以包含多個“Advice”,例如TestAspect
    • 切入點(Pointcut) :匹配連接點的斷言,在AOP中通知和一個切入點表達式關聯。例如,TestAspect中的所有通知所關注的連接點,都由切入點表達式execution(* com.spring.service.*.*(..))來決定
    • 目標對象(Target Object) :被一個或者多個切面所通知的對象。例如,AServcieImpl和BServiceImpl,當然在實際運行時,Spring AOP采用代理實現,實際AOP操作的是TargetObject的代理對象。
    • AOP代理(AOP Proxy) 在Spring AOP中有兩種代理方式,JDK動態代理和CGLIB代理。默認情況下,TargetObject實現了接口時,則采用JDK動態代理,例 如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。強制使用CGLIB代理需要將 <aop:config> 的 proxy-target-class 屬性設為true

    ?????? 通知(Advice)類型

    • 前置通知(Before advice) :在某連接點(JoinPoint)之前執行的通知,但這個通知不能阻止連接點前的執行。ApplicationContext中 在<aop:aspect>里面使用<aop:before>元素進行聲明。例如,TestAspect中的doBefore方 法
    • 后通知(After advice) :當某連接點退出的時候執行的通知(不論是正常返回還是異常退出)。ApplicationContext中在<aop:aspect>里面 使用<aop:after>元素進行聲明。例如,TestAspect中的doAfter方法,所以AOPTest中調用 BServiceImpl.barB拋出異常時,doAfter方法仍然執行
    • 返回后通知(After return advice) :在某連接點正常完成后執行的通知,不包括拋出異常的情況。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素進行聲明。
    • 環繞通知(Around advice) :包圍一個連接點的通知,類似Web中Servlet規范中的Filter的doFilter方法。可以在方法的調用前后完成自定義的行為,也可以選擇不 執行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素進行聲明。例 如,TestAspect中的doAround方法。
    • 拋出異常后通知(After throwing advice) : 在方法拋出異常退出時執行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素進行聲明。例如,TestAspect中的doThrowing方法。

    ?????? 切入點表達式

    • 通常情況下,表達式中使用”execution“就可以滿足大部分的要求。表達式格式如下:
    Java代碼 ?
  • execution(modifiers-pattern??ret-type-pattern?declaring-type-pattern??name-pattern(param-pattern)?throws-pattern?)??
  • modifiers-pattern:方法的操作權限

    ret-type-pattern:返回值

    declaring-type-pattern:方法所在的包

    name-pattern:方法名

    parm-pattern:參數名

    throws-pattern:異常

    其中,除ret-type-pattern和name-pattern之外,其他都是可 選的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值為任意類型;方法名任意;參數不作限制的 所有方法。

    • 通知參數

    可以通過args來綁定參數,這樣就可以在通知(Advice)中訪問具體參數了。例如,<aop:aspect>配置如下

    Java代碼 ?
  • <aop:config>??
  • ????<aop:aspect?id="TestAspect"?ref="aspectBean">??
  • ????????<aop:pointcut?id="businessService"??
  • ????????????expression="execution(*?com.spring.service.*.*(String,..))?and?args(msg,..)"?/>??
  • ????????????<aop:after?pointcut-ref="businessService"?method="doAfter"/>??
  • ????</aop:aspect>??
  • </aop:config>??
  • TestAspect的doAfter方法中就可以訪問msg參數,但這樣以來 AService中的barA()和BServiceImpl中的barB()就不再是連接點,因為execution(* com.spring.service.*.*(String,..))只配置第一個參數為String類型的方法。其中,doAfter方法定義如下:

    Java代碼 ?
  • public?void?doAfter(JoinPoint?jp,String?msg)??
    • ? 訪問當前的連接點

    任何通知(Advice)方法可以將第一個參數定義為 org.aspectj.lang.JoinPoint 類型。JoinPoint 接口提供了一系列有用的方法, 比如 getArgs() (返回方法參數)、getThis() (返回代理對象)、getTarget() (返回目標)、getSignature() (返回正在被通知的方法相關信息)和 toString() (打印出正在被通知的方法的有用信息。

    轉載于:https://www.cnblogs.com/wzyxidian/p/5157893.html

    總結

    以上是生活随笔為你收集整理的AOP举例子的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。