1. 程式人生 > 實用技巧 >基於XML的方式配置AOP

基於XML的方式配置AOP

第一步:把通知類用 bean 標籤配置起來

<bean id="txManager" class="com.atguigu.account.utils.TransactionManager">
    <!-- 注入ConnectionUtils -->
    <property name="connectionUtils" ref="connectionUtils" />
</bean>

第二步:使用 aop:config 宣告 aop 配置

  aop:config: 
        作用:用於宣告開始 aop 的配置  
  <aop:config> 
     
  </aop:config>

第三步:在使用 aop:config標籤裡面配置 aop:aspect切面

  aop:aspect: 
     作用:   用於配置切面。  屬性:   
     id:給切面提供一個唯一標識。   
     ref:引用配置好的通知類 bean 的 id。 
    <aop:aspect id="txAdvice" ref="txManager"> 
          <!--配置通知的型別要寫在此處--> 
    </aop:aspect>

第四步:使用 aop:pointcut 配置切入點表示式

  aop:pointcut:  
        作用:   用於配置切入點表示式。就是指定對哪些類的哪些方法進行增強。  
        屬性:   expression:用於定義切入點表示式。   
        id:用於給切入點表示式提供一個唯一標識 
  <!--配置通用切入點表示式,需要將該標籤放置在通知之前-->
  <aop:pointcut id="pt1" expression="execution(* com.atguigu.account.service.impl.*.*(..))" />

第五步:使用 aop:xxx 配置對應的通知型別

<aop:config>
    <!--配置通用切入點表示式-->
    <aop:pointcut id="pt1" expression="execution(* com.atguigu.account.service.impl.*.*(..))" />
    <aop:aspect id="txAdvice" ref="txManager">
        <!--配置前置通知:開啟事務-->
        <aop:before method="beginTransaction" pointcut-ref="pt1" />
        <!--配置後置通知:提交事務-->
        <aop:after-returning method="commit" pointcut-ref="pt1" />
        <!--配置異常通知:回滾事務-->
        <aop:after-throwing method="rollback" pointcut-ref="pt1" />
        <!--配置最終通知:釋放連線-->
        <aop:after method="release" pointcut-ref="pt1" />
    </aop:aspect>
</aop:config>

六、詳細解析

  aop:before 
   作用:   用於配置前置通知。指定增強的方法在切入點方法之前執行   
   屬性:  
        method:用於指定通知類中的增強方法名稱   
        ponitcut-ref:用於指定切入點的表示式的引用   
        poinitcut:用於指定切入點表示式   
        執行時間點:切入點方法執行之前執行 
   <aop:before method="beginTransaction" pointcut-ref="pt1"/> 


    aop:after-returning 
        作用:   用於配置後置通知  屬性:   
        method:指定通知中方法的名稱。   
        pointct:定義切入點表示式   
        pointcut-ref:指定切入點表示式的引用  
        執行時間點:   切入點方法正常執行之後。它和異常通知只能有一個執行 
    <aop:after-returning method="commit" pointcut-ref="pt1"/> 


    aop:after-throwing 
         作用:   用於配置異常通知  屬性:   
         method:指定通知中方法的名稱。   
         pointct:定義切入點表示式   
         pointcut-ref:指定切入點表示式的引用  
         執行時間點:   切入點方法執行產生異常後執行。它和後置通知只能執行一個 
   <aop:after-throwing method="rollback" pointcut-ref="pt1"/>


   aop:after 
         作用:   用於配置最終通知  屬性:   
         method:指定通知中方法的名稱。   
         pointct:定義切入點表示式   
         pointcut-ref:指定切入點表示式的引用  
         執行時間點:   無論切入點方法執行時是否有異常,它都會在其後面執行。 
   <aop:after method="release" pointcut-ref="pt1"/> 



  aop:around:  
         作用:   用於配置環繞通知  
         屬性:   
              method:指定通知中方法的名稱。   
              pointct:定義切入點表示式   
              pointcut-ref:指定切入點表示式的引用  
         說明:   它是 spring 框架為我們提供的一種可以在程式碼中手動控制增強程式碼什麼時候執行的方式。  
         注意:   通常情況下,環繞通知都是獨立使用的 

七、環繞通知

  /** 
   * 環繞通知  * @param pjp 
   *  spring 框架為我們提供了一個介面:ProceedingJoinPoint,它可以作為環繞通知的方法引數。  
   *  在環繞通知執行時,spring 框架會為我們提供該介面的實現類物件,我們直接使用就行。  
   * @return  
   */ 
   public Object transactionAround(ProceedingJoinPoint pjp) { 
         //定義返回值  
        Object rtValue = null;  
        try {   
        //獲取方法執行所需的引數   
        Object[] args = pjp.getArgs(); 
        
        //前置通知:開啟事務   
        beginTransaction(); 
       
        //執行方法   
        rtValue = pjp.proceed(args); 
       
        //後置通知:提交事務   
        commit();  
       }catch(Throwable e) {   
        //異常通知:回滾事務   
        rollback();   
        e.printStackTrace();  
        }finally { 
          //最終通知:釋放資源   
              release();  
        }  
              return rtValue; 
     }