Spring Aop實例之xml配置

上篇博文《3幅圖讓你瞭解Spring AOP》中介紹了aop通知類型,AOP的配置方式有2種方式:xml配置和AspectJ註解方式。今天咱們就來實踐一下xml配置方式。html

http://blog.csdn.net/xiaoxian8023/article/details/17258933java

 

      我採用的jdk代理,因此首先將接口和實現類代碼附上spring

 

[java]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
  1. package com.tgb.aop;  
  2.   
  3. public interface UserManager {  
  4.   
  5.     public String findUserById(int userId);  
  6. }  
  7.   
  8.   
  9. package com.tgb.aop;  
  10.   
  11. public class UserManagerImpl implements UserManager {  
  12.   
  13.     public String findUserById(int userId) {  
  14.         System.out.println("---------UserManagerImpl.findUserById()--------");  
  15.         if (userId <= 0) {  
  16.             throw new IllegalArgumentException("該用戶不存在!");   
  17.         }  
  18.         return "張三";  
  19.     }  
  20. }  


       單獨寫一個Advice通知類進行測試。這個通知類能夠換成安全性檢測、日誌管理等等。express

 

[java]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
  1. package com.tgb.aop;  
  2.   
  3. import org.aspectj.lang.JoinPoint;  
  4. import org.aspectj.lang.ProceedingJoinPoint;  
  5.   
  6. /** 
  7.  * Advice通知類 
  8.  * 測試after,before,around,throwing,returning Advice. 
  9.  * @author Admin 
  10.  * 
  11.  */  
  12. public class XMLAdvice {  
  13.   
  14.     /** 
  15.      * 在覈心業務執行前執行,不能阻止核心業務的調用。 
  16.      * @param joinPoint 
  17.      */  
  18.     private void doBefore(JoinPoint joinPoint) {  
  19.         System.out.println("-----doBefore().invoke-----");  
  20.         System.out.println(" 此處意在執行核心業務邏輯前,作一些安全性的判斷等等");  
  21.         System.out.println(" 可經過joinPoint來獲取所須要的內容");  
  22.         System.out.println("-----End of doBefore()------");  
  23.     }  
  24.       
  25.     /** 
  26.      * 手動控制調用核心業務邏輯,以及調用前和調用後的處理, 
  27.      *  
  28.      * 注意:當核心業務拋異常後,當即退出,轉向After Advice 
  29.      * 執行完畢After Advice,再轉到Throwing Advice 
  30.      * @param pjp 
  31.      * @return 
  32.      * @throws Throwable 
  33.      */  
  34.     private Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
  35.         System.out.println("-----doAround().invoke-----");  
  36.         System.out.println(" 此處能夠作相似於Before Advice的事情");  
  37.           
  38.         //調用核心邏輯  
  39.         Object retVal = pjp.proceed();  
  40.           
  41.         System.out.println(" 此處能夠作相似於After Advice的事情");  
  42.         System.out.println("-----End of doAround()------");  
  43.         return retVal;  
  44.     }  
  45.   
  46.     /** 
  47.      * 核心業務邏輯退出後(包括正常執行結束和異常退出),執行此Advice 
  48.      * @param joinPoint 
  49.      */  
  50.     private void doAfter(JoinPoint joinPoint) {  
  51.         System.out.println("-----doAfter().invoke-----");  
  52.         System.out.println(" 此處意在執行核心業務邏輯以後,作一些日誌記錄操做等等");  
  53.         System.out.println(" 可經過joinPoint來獲取所須要的內容");  
  54.         System.out.println("-----End of doAfter()------");  
  55.     }  
  56.       
  57.     /** 
  58.      * 核心業務邏輯調用正常退出後,無論是否有返回值,正常退出後,均執行此Advice 
  59.      * @param joinPoint 
  60.      */  
  61.     private void doReturn(JoinPoint joinPoint) {  
  62.         System.out.println("-----doReturn().invoke-----");  
  63.         System.out.println(" 此處能夠對返回值作進一步處理");  
  64.         System.out.println(" 可經過joinPoint來獲取所須要的內容");  
  65.         System.out.println("-----End of doReturn()------");  
  66.     }  
  67.       
  68.     /** 
  69.      * 核心業務邏輯調用異常退出後,執行此Advice,處理錯誤信息 
  70.      * @param joinPoint 
  71.      * @param ex 
  72.      */  
  73.     private void doThrowing(JoinPoint joinPoint,Throwable ex) {  
  74.         System.out.println("-----doThrowing().invoke-----");  
  75.         System.out.println(" 錯誤信息:"+ex.getMessage());  
  76.         System.out.println(" 此處意在執行核心業務邏輯出錯時,捕獲異常,並可作一些日誌記錄操做等等");  
  77.         System.out.println(" 可經過joinPoint來獲取所須要的內容");  
  78.         System.out.println("-----End of doThrowing()------");  
  79.     }  
  80. }  


       只有Advice還不行,還須要在application-config.xml中進行配置:安全

 

 

[html]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  10.                
  11.       
  12.     <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>  
  13.       
  14.     <!--<bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>-->  
  15.     <bean id="xmlHandler" class="com.tgb.aop.XMLAdvice" />  
  16.     <aop:config>  
  17.         <aop:aspect id="aspect" ref="xmlHandler">  
  18.             <aop:pointcut id="pointUserMgr" expression="execution(* com.tgb.aop.*.find*(..))"/>  
  19.               
  20.             <aop:before method="doBefore"  pointcut-ref="pointUserMgr"/>  
  21.             <aop:after method="doAfter"  pointcut-ref="pointUserMgr"/>  
  22.             <aop:around method="doAround"  pointcut-ref="pointUserMgr"/>  
  23.             <aop:after-returning method="doReturn"  pointcut-ref="pointUserMgr"/>  
  24.             <aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="pointUserMgr"/>  
  25.               
  26.         </aop:aspect>  
  27.     </aop:config>  
  28. </beans>  


       編一個客戶端類進行測試一下:app

 

 

[java]  view plain copy 在CODE上查看代碼片 派生到個人代碼片
 
    1. package com.tgb.aop;  
    2.   
    3. import org.springframework.beans.factory.BeanFactory;  
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    5.   
    6. public class Client {  
    7.   
    8.     public static void main(String[] args) {  
    9.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
    10.         UserManager userManager = (UserManager)factory.getBean("userManager");  
    11.           
    12.         //能夠查找張三  
    13.         userManager.findUserById(1);  
    14.           
    15.         System.out.println("=====我==是==分==割==線=====");  
    16.   
    17.         try {  
    18.             // 查不到數據,會拋異常,異常會被AfterThrowingAdvice捕獲  
    19.             userManager.findUserById(0);  
    20.         } catch (IllegalArgumentException e) {  
    21.         }  
    22.     }  
    23. }    
    24. 結果如圖:

值得注意的是Around與Before和After的執行順序。3者的執行順序取決於在xml中的配置順序。圖中標記了3塊,分別對應Before,Around,After。其中②中包含有③。這是由於aop:after配置到了aop:around的前面,若是2者調換一下位置,這三塊就會分開獨立顯示。若是配置順序是aop:after  -> aop:around ->aop:before,那麼①和③都會包含在②中。這種狀況的產生是因爲Around的特殊性,它能夠作相似於Before和After的操做。當安全性的判斷不經過時,能夠阻止核心業務邏輯的調用,這是Before作不到的。測試

使用xml能夠對aop進行集中配置。很方便而簡單。能夠對全部的aop進行配置,固然也能夠分開到單獨的xml中進行配置。當需求變更時,不用修改代碼,只要從新配置aop,就能夠完成修改操做。spa

相關文章
相關標籤/搜索