最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Spring Aop實例之xml配置

 更新時間:2017年04月05日 11:22:54   作者:龍軒  
本篇文章主要介紹了詳解Spring Aop實例之xml配置,使用xml可以對aop進行集中配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下

AOP的配置方式有2種方式:xml配置和AspectJ注解方式。今天我們就來實踐一下xml配置方式。

我采用的jdk代理,所以首先將接口和實現(xiàn)類代碼附上

package com.tgb.aop; 
public interface UserManager { 
 
  public String findUserById(int userId); 
} 
 
 
package com.tgb.aop; 
 
public class UserManagerImpl implements UserManager { 
 
  public String findUserById(int userId) { 
    System.out.println("---------UserManagerImpl.findUserById()--------"); 
    if (userId <= 0) { 
      throw new IllegalArgumentException("該用戶不存在!");  
    } 
    return "張三"; 
  } 
}

單獨寫一個Advice通知類進行測試。這個通知類可以換成安全性檢測、日志管理等等。

package com.tgb.aop; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.ProceedingJoinPoint; 
 /** 
 * Advice通知類 
 * 測試after,before,around,throwing,returning Advice. 
 * @author Admin 
 * 
 */ 
public class XMLAdvice { 
 
  /** 
   * 在核心業(yè)務執(zhí)行前執(zhí)行,不能阻止核心業(yè)務的調用。 
   * @param joinPoint 
   */ 
  private void doBefore(JoinPoint joinPoint) { 
    System.out.println("-----doBefore().invoke-----"); 
    System.out.println(" 此處意在執(zhí)行核心業(yè)務邏輯前,做一些安全性的判斷等等"); 
    System.out.println(" 可通過joinPoint來獲取所需要的內(nèi)容"); 
    System.out.println("-----End of doBefore()------"); 
  } 
   
  /** 
   * 手動控制調用核心業(yè)務邏輯,以及調用前和調用后的處理, 
   * 
   * 注意:當核心業(yè)務拋異常后,立即退出,轉向After Advice 
   * 執(zhí)行完畢After Advice,再轉到Throwing Advice 
   * @param pjp 
   * @return 
   * @throws Throwable 
   */ 
  private Object doAround(ProceedingJoinPoint pjp) throws Throwable { 
    System.out.println("-----doAround().invoke-----"); 
    System.out.println(" 此處可以做類似于Before Advice的事情"); 
     
    //調用核心邏輯 
    Object retVal = pjp.proceed(); 
     
    System.out.println(" 此處可以做類似于After Advice的事情"); 
    System.out.println("-----End of doAround()------"); 
    return retVal; 
  } 
 
  /** 
   * 核心業(yè)務邏輯退出后(包括正常執(zhí)行結束和異常退出),執(zhí)行此Advice 
   * @param joinPoint 
   */ 
  private void doAfter(JoinPoint joinPoint) { 
    System.out.println("-----doAfter().invoke-----"); 
    System.out.println(" 此處意在執(zhí)行核心業(yè)務邏輯之后,做一些日志記錄操作等等"); 
    System.out.println(" 可通過joinPoint來獲取所需要的內(nèi)容"); 
    System.out.println("-----End of doAfter()------"); 
  } 
   
  /** 
   * 核心業(yè)務邏輯調用正常退出后,不管是否有返回值,正常退出后,均執(zhí)行此Advice 
   * @param joinPoint 
   */ 
  private void doReturn(JoinPoint joinPoint) { 
    System.out.println("-----doReturn().invoke-----"); 
    System.out.println(" 此處可以對返回值做進一步處理"); 
    System.out.println(" 可通過joinPoint來獲取所需要的內(nèi)容"); 
    System.out.println("-----End of doReturn()------"); 
  } 
   
  /** 
   * 核心業(yè)務邏輯調用異常退出后,執(zhí)行此Advice,處理錯誤信息 
   * @param joinPoint 
   * @param ex 
   */ 
  private void doThrowing(JoinPoint joinPoint,Throwable ex) { 
    System.out.println("-----doThrowing().invoke-----"); 
    System.out.println(" 錯誤信息:"+ex.getMessage()); 
    System.out.println(" 此處意在執(zhí)行核心業(yè)務邏輯出錯時,捕獲異常,并可做一些日志記錄操作等等"); 
    System.out.println(" 可通過joinPoint來獲取所需要的內(nèi)容"); 
    System.out.println("-----End of doThrowing()------"); 
  } 
} 

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

<?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"> 
        
   
  <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/> 
   
  <!--<bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>--> 
  <bean id="xmlHandler" class="com.tgb.aop.XMLAdvice" /> 
  <aop:config> 
    <aop:aspect id="aspect" ref="xmlHandler"> 
      <aop:pointcut id="pointUserMgr" expression="execution(* com.tgb.aop.*.find*(..))"/>        
      <aop:before method="doBefore" pointcut-ref="pointUserMgr"/> 
      <aop:after method="doAfter" pointcut-ref="pointUserMgr"/> 
      <aop:around method="doAround" pointcut-ref="pointUserMgr"/> 
      <aop:after-returning method="doReturn" pointcut-ref="pointUserMgr"/> 
      <aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="pointUserMgr"/>        
    </aop:aspect> 
  </aop:config> 
</beans> 

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

package com.tgb.aop;  
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
public class Client { 
 
  public static void main(String[] args) { 
    BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 
    UserManager userManager = (UserManager)factory.getBean("userManager"); 
     
    //可以查找張三 
    userManager.findUserById(1); 
     
    System.out.println("=====我==是==分==割==線====="); 
 
    try { 
      // 查不到數(shù)據(jù),會拋異常,異常會被AfterThrowingAdvice捕獲 
      userManager.findUserById(0); 
    } catch (IllegalArgumentException e) { 
    } 
  } 
} 

結果如圖:

值得注意的是Around與Before和After的執(zhí)行順序。3者的執(zhí)行順序取決于在xml中的配置順序。圖中標記了3塊,分別對應Before,Around,After。其中②中包含有③。這是因為aop:after配置到了aop:around的前面,如果2者調換一下位置,這三塊就會分開獨立顯示。如果配置順序是aop:after  -> aop:around ->aop:before,那么①和③都會包含在②中。這種情況的產(chǎn)生是由于Around的特殊性,它可以做類似于Before和After的操作。當安全性的判斷不通過時,可以阻止核心業(yè)務邏輯的調用,這是Before做不到的。

  

 

使用xml可以對aop進行集中配置。很方便而簡單??梢詫λ械腶op進行配置,當然也可以分開到單獨的xml中進行配置。當需求變動時,不用修改代碼,只要重新配置aop,就可以完成修改操作。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 多用多學之Java中的Set,List,Map詳解

    多用多學之Java中的Set,List,Map詳解

    下面小編就為大家?guī)硪黄嘤枚鄬W之Java中的Set,List,Map詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • SpringCloud?LoadBalancerClient?負載均衡原理解析

    SpringCloud?LoadBalancerClient?負載均衡原理解析

    LoadBalancerClient?是?SpringCloud?提供的一種負載均衡客戶端,Ribbon?負載均衡組件內(nèi)部也是集成了?LoadBalancerClient?來實現(xiàn)負載均衡,本文給大家深入解析?LoadBalancerClient?接口源碼,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • 解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式)

    解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式)

    這篇文章主要介紹了解讀SpringBoot接收List<Bean>參數(shù)問題(POST請求方式),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • java地理坐標系及投影間轉換代碼示例

    java地理坐標系及投影間轉換代碼示例

    在地圖投影中,經(jīng)常需要將坐標從不同的坐標系之間進行轉換,下面這篇文章主要給大家介紹了關于java地理坐標系及投影間轉換的相關資料,需要的朋友可以參考下
    2024-08-08
  • Java編程實現(xiàn)統(tǒng)計數(shù)組中各元素出現(xiàn)次數(shù)的方法

    Java編程實現(xiàn)統(tǒng)計數(shù)組中各元素出現(xiàn)次數(shù)的方法

    這篇文章主要介紹了Java編程實現(xiàn)統(tǒng)計數(shù)組中各元素出現(xiàn)次數(shù)的方法,涉及java針對數(shù)組的遍歷、比較、運算等相關操作技巧,需要的朋友可以參考下
    2017-07-07
  • Java中BigInteger與BigDecimal類用法總結

    Java中BigInteger與BigDecimal類用法總結

    在Java中有兩個用于大數(shù)字運算的類,分別是java.math.BigInteger類 和 java.math.BigDecimal類,這兩個類都可以用于高精度計算,BigInteger類是針對整型大數(shù)字的處理類,而BigDecimal類是針對大小數(shù)的處理類,接下來帶大家來學習一下,在Java中如何處理大數(shù)字
    2023-05-05
  • java微信掃碼支付模式一線下支付功能實現(xiàn)

    java微信掃碼支付模式一線下支付功能實現(xiàn)

    本篇文章主要介紹了JAVA微信掃碼支付模式一線下支付功能實現(xiàn),具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • SpringBoot2.x集成Dozer的示例代碼

    SpringBoot2.x集成Dozer的示例代碼

    本文主要介紹了SpringBoot2.x集成Dozer的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • SpringBoot配置外部靜態(tài)資源映射問題

    SpringBoot配置外部靜態(tài)資源映射問題

    這篇文章主要介紹了SpringBoot配置外部靜態(tài)資源映射問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Maven項目外部jar包導入的實現(xiàn)示例

    Maven項目外部jar包導入的實現(xiàn)示例

    在Maven項目里,我們經(jīng)常需要導入jar包依賴,本文主要介紹了Maven項目外部jar包導入的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08

最新評論

修文县| 灵石县| 新津县| 阳高县| 威宁| 南雄市| 镇平县| 临西县| 罗田县| 新闻| 永泰县| 贺兰县| 昌邑市| 山阳县| 营口市| 绥芬河市| 安宁市| 安宁市| 大姚县| 乌审旗| 天台县| 化州市| 台州市| 上饶市| 龙海市| 启东市| 琼中| 卢湾区| 广宗县| 堆龙德庆县| 望都县| 桑日县| 孟津县| 喀喇| 石渠县| 民乐县| 察雅县| 遂昌县| 和龙市| 乐亭县| 来宾市|