一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring Aop實例之xml配置

詳解Spring Aop實例之xml配置

2020-09-06 15:12龍軒 Java教程

本篇文章主要介紹了詳解Spring Aop實例之xml配置,使用xml可以對aop進行集中配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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通知類進行測試。這個通知類可以換成安全性檢測、日志管理等等。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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 {
 
  /**
   * 在核心業務執行前執行,不能阻止核心業務的調用。
   * @param joinpoint
   */
  private void dobefore(joinpoint joinpoint) {
    system.out.println("-----dobefore().invoke-----");
    system.out.println(" 此處意在執行核心業務邏輯前,做一些安全性的判斷等等");
    system.out.println(" 可通過joinpoint來獲取所需要的內容");
    system.out.println("-----end of dobefore()------");
  }
   
  /**
   * 手動控制調用核心業務邏輯,以及調用前和調用后的處理,
   *
   * 注意:當核心業務拋異常后,立即退出,轉向after advice
   * 執行完畢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;
  }
 
  /**
   * 核心業務邏輯退出后(包括正常執行結束和異常退出),執行此advice
   * @param joinpoint
   */
  private void doafter(joinpoint joinpoint) {
    system.out.println("-----doafter().invoke-----");
    system.out.println(" 此處意在執行核心業務邏輯之后,做一些日志記錄操作等等");
    system.out.println(" 可通過joinpoint來獲取所需要的內容");
    system.out.println("-----end of doafter()------");
  }
   
  /**
   * 核心業務邏輯調用正常退出后,不管是否有返回值,正常退出后,均執行此advice
   * @param joinpoint
   */
  private void doreturn(joinpoint joinpoint) {
    system.out.println("-----doreturn().invoke-----");
    system.out.println(" 此處可以對返回值做進一步處理");
    system.out.println(" 可通過joinpoint來獲取所需要的內容");
    system.out.println("-----end of doreturn()------");
  }
   
  /**
   * 核心業務邏輯調用異常退出后,執行此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(" 此處意在執行核心業務邏輯出錯時,捕獲異常,并可做一些日志記錄操作等等");
    system.out.println(" 可通過joinpoint來獲取所需要的內容");
    system.out.println("-----end of dothrowing()------");
  }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 {
      // 查不到數據,會拋異常,異常會被afterthrowingadvice捕獲
      usermanager.finduserbyid(0);
    } catch (illegalargumentexception e) {
    }
  }
}

結果如圖:

詳解Spring Aop實例之xml配置

詳解Spring Aop實例之xml配置

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

詳解Spring Aop實例之xml配置  

詳解Spring Aop實例之xml配置

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/xiaoxian8023/article/details/17258933

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品一区heyzo在线播放 | 免费视频网 | 亚洲2017久无码 | 韩国三级在线观看 完整版 韩国三级视频网站 | 深夜国产在线 | 亚洲国产第一区二区香蕉日日 | 逼水真多| 精品一卡2卡3卡4卡5卡亚洲 | 欧美日韩国产精品综合 | 窝窝影院午夜色在线视频 | 国产欧美亚洲精品第一页青草 | 日本无卡视频 | 日韩欧美国产一区 | 肥胖女性大bbbbbb视频女厕 | 欧美日韩一区二区综合 | 欧美日韩一二三区免费视频观看 | 胸大的姑娘中文字幕视频 | 男人操美女逼视频 | 公交车揉捏大乳呻吟喘娇 | 免费观看在线观看 | 色综合网天天综合色中文男男 | 国产japanese孕妇孕交 | 男人把大ji巴放进女人小说 | 亚洲欧美日韩国产一区二区精品 | 日本视频在线观看 | 日本中文字幕在线观看视频 | 日本性生活免费看 | 九九成人免费视频 | 久久永久影院免费 | 日本久久热 | 男人天堂网www | 女八把屁股扒开让男生添 | 大ji巴好好爽好深网站 | 俄罗斯妈妈k8影院在线观看 | 白丝女榨干蹂躏我 | 99精品视频在线观看 | 痴mu动漫成年动漫在线观看 | 国产一区二区三区日韩 | 经典千人斩一区二区视频 | 亚洲福利视频在线观看 | 91精品91 |