上篇博文《Spring Aop執行個體之xml配置》中,講解了xml配置方式,今天來說說AspectJ註解方式去配置spring aop。
依舊採用的jdk代理,介面和實作類別代碼請參考上篇博文。主要是將Aspect類分享一下:
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 * */@Aspectpublic 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()------"); } }
application-config.xml中,只需要配置商務邏輯bean和Aspect bean,並啟用Aspect註解即可:
<?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>
結果如圖:
通過測試的發現AroundAdvice、BeforeAdvice、AfterAdvice、ReturningAdvice的執行順序是根據註解的順序而定的。但是有時候修改了順序,結果卻沒有變化,可能是緩衝的緣故。前幾天我也遇到了這樣的問題,不過今天再測試了一下,發現執行順序又跟註解的順序一致了。
xml 和 Annotation 註解都可以作為配置項,對Spring AoP進行組態管理,那麼它們各自都有什麼優缺點呢。
首先說說 xml 。目前 web 應用中幾乎都使用 xml 作為配置項,例如我們常用的架構 Struts、Spring、Hibernate 等等都採用 xml 作為配置。xml 之所以這麼流行,是因為它的很多優點是其它技術的配置所無法替代的:
xml 作為可延伸標記語言 (XML)最大的優勢在於開發人員能夠為軟體量身定製適用的標記,使代碼更加通俗易懂。 利用 xml 配置能使軟體更具擴充性。例如 Spring 將 class 間的依賴配置在 xml 中,最大限度地提升應用的可擴充性。 具有成熟的驗證機制確保程式正確性。利用 Schema 或 DTD 可以對 xml 的正確性進行驗證,避免了非法的配置導致應用程式出錯。 修改配置而無需變動現有程式。 雖然有如此多的好處,但畢竟沒有什麼萬能的東西,xml 也有自身的缺點。
需要解析工具或類庫的支援。 解析 xml 勢必會影響應用程式效能,佔用系統資源。 設定檔過多導致管理變得困難。 編譯期無法對其配置項的正確性進行驗證,或要查錯只能在運行期。 IDE 無法驗證配置項的正確性無能為力。 查錯變得困難。往往配置的一個手誤導致莫名其妙的錯誤。 開發人員不得不同時維護代碼和設定檔,開發效率變得低下。 配置項與代碼間存在潛規則。改變了任何一方都有可能影響另外一方。 讓我們來看看 Annotation 的優點。
儲存在 class 檔案中,降低維護成本。 無需工具支援,無需解析。 編譯期即可驗證正確性,查錯變得容易。 提升開發效率。 同樣 Annotation 也不是萬能的,它也有很多缺點。
若要對配置項進行修改,不得不修改 Java 檔案,重新編譯打包應用。 配置項編碼在 Java 檔案中,可擴充性差。 總結:沒有一個事物是萬能的,同樣 xml 和 Java Annotation 都有各自的優缺點。通過以上對比,細心的讀者可能已經發現它們的優缺點恰恰是互補的。xml 的強項是 Annotation 所不具備的,而 Annotation 的優勢也是 xml 所欠缺的。這也正是時下流行的 xml + Annotation 配置的原因所在。平衡才是王道呀。