當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Aop实例之AspectJ注解配置
生活随笔
收集整理的這篇文章主要介紹了
Spring Aop实例之AspectJ注解配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上篇博文《Spring Aop實例之xml配置》中,講解了xml配置方式,今天來說說AspectJ注解方式去配置spring aop。
package?com.tgb.aop;?? ?? 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.DeclareParents;?? import?org.aspectj.lang.annotation.Pointcut;?? ?? /**? ?*?測試after,before,around,throwing,returning?Advice.? ?*?@author?Admin? ?*? ?*/?? @Aspect?? public?class?AspceJAdvice?{?? ?? ????/**? ?????*?Pointcut? ?????*?定義Pointcut,Pointcut的名稱為aspectjMethod(),此方法沒有返回值和參數? ?????*?該方法就是一個標識,不進行調用? ?????*/?? ????@Pointcut("execution(*?find*(..))")?? ????private?void?aspectjMethod(){};?? ?????? ????/**?? ?????*?Before? ?????*?在核心業務執行前執行,不能阻止核心業務的調用。? ?????*?@param?joinPoint?? ?????*/???? ????@Before("aspectjMethod()")???? ????public?void?beforeAdvice(JoinPoint?joinPoint)?{???? ????????System.out.println("-----beforeAdvice().invoke-----");?? ????????System.out.println("?此處意在執行核心業務邏輯前,做一些安全性的判斷等等");?? ????????System.out.println("?可通過joinPoint來獲取所需要的內容");?? ????????System.out.println("-----End?of?beforeAdvice()------");?? ????}?? ?????? ????/**?? ?????*?After?? ?????*?核心業務邏輯退出后(包括正常執行結束和異常退出),執行此Advice? ?????*?@param?joinPoint? ?????*/?? ????@After(value?=?"aspectjMethod()")???? ????public?void?afterAdvice(JoinPoint?joinPoint)?{???? ????????System.out.println("-----afterAdvice().invoke-----");?? ????????System.out.println("?此處意在執行核心業務邏輯之后,做一些日志記錄操作等等");?? ????????System.out.println("?可通過joinPoint來獲取所需要的內容");?? ????????System.out.println("-----End?of?afterAdvice()------");?? ????}???? ?? ????/**?? ?????*?Around?? ?????*?手動控制調用核心業務邏輯,以及調用前和調用后的處理,? ?????*?? ?????*?注意:當核心業務拋異常后,立即退出,轉向AfterAdvice? ?????*?執行完AfterAdvice,再轉到ThrowingAdvice? ?????*?@param?pjp? ?????*?@return? ?????*?@throws?Throwable? ?????*/??? ????@Around(value?=?"aspectjMethod()")???? ????public?Object?aroundAdvice(ProceedingJoinPoint?pjp)?throws?Throwable?{???? ????????System.out.println("-----aroundAdvice().invoke-----");?? ????????System.out.println("?此處可以做類似于Before?Advice的事情");?? ?????????? ????????//調用核心邏輯?? ????????Object?retVal?=?pjp.proceed();?? ????????System.out.println("?此處可以做類似于After?Advice的事情");?? ????????System.out.println("-----End?of?aroundAdvice()------");?? ????????return?retVal;?? ????}???? ?????? ????/**?? ?????*?AfterReturning?? ?????*?核心業務邏輯調用正常退出后,不管是否有返回值,正常退出后,均執行此Advice? ?????*?@param?joinPoint? ?????*/??? ????@AfterReturning(value?=?"aspectjMethod()",?returning?=?"retVal")???? ????public?void?afterReturningAdvice(JoinPoint?joinPoint,?String?retVal)?{???? ????????System.out.println("-----afterReturningAdvice().invoke-----");?? ????????System.out.println("Return?Value:?"?+?retVal);??? ????????System.out.println("?此處可以對返回值做進一步處理");?? ????????System.out.println("?可通過joinPoint來獲取所需要的內容");?? ????????System.out.println("-----End?of?afterReturningAdvice()------");?? ????}?? ?????? ????/**? ?????*?核心業務邏輯調用異常退出后,執行此Advice,處理錯誤信息? ?????*?? ?????*?注意:執行順序在Around?Advice之后? ?????*?@param?joinPoint? ?????*?@param?ex? ?????*/?? ????@AfterThrowing(value?=?"aspectjMethod()",?throwing?=?"ex")???? ????public?void?afterThrowingAdvice(JoinPoint?joinPoint,?Exception?ex)?{???? ????????System.out.println("-----afterThrowingAdvice().invoke-----");?? ????????System.out.println("?錯誤信息:"+ex.getMessage());?? ????????System.out.println("?此處意在執行核心業務邏輯出錯時,捕獲異常,并可做一些日志記錄操作等等");?? ????????System.out.println("?可通過joinPoint來獲取所需要的內容");?? ????????System.out.println("-----End?of?afterThrowingAdvice()------");???? ????}???? }??
<?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"?? ?????????xmlns:tx="http://www.springframework.org/schema/tx"?? ?????????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.0.xsd?? ???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">?? ????????????? ????<!--?啟用AspectJ對Annotation的支持?-->????????? ????<aop:aspectj-autoproxy/>????????????? ?????? ????<bean?id="userManager"?class="com.tgb.aop.UserManagerImpl"/>?? ????<bean?id="aspcejHandler"?class="com.tgb.aop.AspceJAdvice"/>?? ?????? </beans>??
? ? ? ?結果如圖:
? ? ? ?依舊采用的jdk代理,接口和實現類代碼請參考上篇博文。主要是將Aspect類分享一下:
[java] view plain copy print?? ? ? ?application-config.xml中,只需要配置業務邏輯bean和Aspect bean,并啟用Aspect注解即可:
[html] view plain copy print?? ? ? ?結果如圖:
? ? ? ?通過測試的發現AroundAdvice、BeforeAdvice、AfterAdvice、ReturningAdvice的執行順序是根據注解的順序而定的。但是有時候修改了順序,結果卻沒有變化,可能是緩存的緣故。前幾天我也遇到了這樣的問題,不過今天再測試了一下,發現執行順序又跟注解的順序一致了。
? ? ? ?xml 和 Annotation 注解都可以作為配置項,對Spring AoP進行配置管理,那么它們各自都有什么優缺點呢?
首先說說 xml 。目前 web 應用中幾乎都使用 xml 作為配置項,例如我們常用的框架 Struts、Spring、Hibernate 等等都采用 xml 作為配置。xml 之所以這么流行,是因為它的很多優點是其它技術的配置所無法替代的:
- xml 作為可擴展標記語言最大的優勢在于開發者能夠為軟件量身定制適用的標記,使代碼更加通俗易懂。
- 利用 xml 配置能使軟件更具擴展性。例如 Spring 將 class 間的依賴配置在 xml 中,最大限度地提升應用的可擴展性。
- 具有成熟的驗證機制確保程序正確性。利用 Schema 或 DTD 可以對 xml 的正確性進行驗證,避免了非法的配置導致應用程序出錯。
- 修改配置而無需變動現有程序。
- 需要解析工具或類庫的支持。
- 解析 xml 勢必會影響應用程序性能,占用系統資源。
- 配置文件過多導致管理變得困難。
- 編譯期無法對其配置項的正確性進行驗證,或要查錯只能在運行期。
- IDE 無法驗證配置項的正確性無能為力。
- 查錯變得困難。往往配置的一個手誤導致莫名其妙的錯誤。
- 開發人員不得不同時維護代碼和配置文件,開發效率變得低下。
- 配置項與代碼間存在潛規則。改變了任何一方都有可能影響另外一方。
- 保存在 class 文件中,降低維護成本。
- 無需工具支持,無需解析。
- 編譯期即可驗證正確性,查錯變得容易。
- 提升開發效率。
- 若要對配置項進行修改,不得不修改 Java 文件,重新編譯打包應用。
- 配置項編碼在 Java 文件中,可擴展性差。
總結
以上是生活随笔為你收集整理的Spring Aop实例之AspectJ注解配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Aop实例之xml配置
- 下一篇: 一对多单向关联映射