国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

服務器之家 - 編程語言 - Java教程 - spring aop之鏈式調用的實現

spring aop之鏈式調用的實現

2021-07-16 16:00niocoder Java教程

這篇文章主要介紹了spring aop之鏈式調用的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

概述

aopaspect orient programming),我們一般稱為面向方面(切面)編程,作為面向對象的一種補充,用于處理系統中分布于各個模塊的橫切關注點,比如事務管理、日志、緩存等等。 spring aop采用的是動態代理,在運行期間對業務方法進行增強,所以不會生成新類,spring aop提供了對jdk動態代理的支持以及cglib的支持。本章我們不關注aop代理類的實現,我簡單實現一個指定次序的鏈式調用

實現鏈式調用的

methodinterceptor定義攔截器鏈,methodinvocation 遞歸進入下一個攔截器鏈中。類圖如下:

spring aop之鏈式調用的實現

methodinterceptor

?
1
2
3
4
public interface methodinterceptor {
 
  object invoke(methodinvocation invocation) throws throwable;
}

methodinvocation

?
1
2
3
4
public interface methodinvocation {
 
  object proceed() throws throwable;
}

abstractaspectjadvice

抽象類,實現methodinterceptor

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public abstract class abstractaspectjadvice implements methodinterceptor{
 
  private method advicemethod;
 
  private object adviceobject;
 
  public abstractaspectjadvice(method advicemethod, object adviceobject) {
    this.advicemethod = advicemethod;
    this.adviceobject = adviceobject;
  }
 
  public method getadvicemethod() {
    return this.advicemethod;
  }
 
  public void invokeadvicemethod() throws throwable {
    advicemethod.invoke(adviceobject);
  }
}

aspectjbeforeadvice

前置通知

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class aspectjbeforeadvice extends abstractaspectjadvice {
 
  public aspectjbeforeadvice(method method, object adviceobject) {
    super(method, adviceobject);
  }
 
  @override
  public object invoke(methodinvocation invocation) throws throwable{
    this.invokeadvicemethod();
    object o = invocation.proceed();
    return o;
  }
}

aspectjafterreturningadvice

后置通知

?
1
2
3
4
5
6
7
8
9
10
11
12
13
public class aspectjafterreturningadvice extends abstractaspectjadvice {
 
  public aspectjafterreturningadvice(method method, object adviceobject) {
    super(method, adviceobject);
  }
 
  @override
  public object invoke(methodinvocation invocation) throws throwable{
    object o = invocation.proceed();
    this.invokeadvicemethod();
    return o;
  }
}

reflectivemethodinvocation

實現methodinvocationproceed()方法遞歸實現鏈式調用。

?
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
public class reflectivemethodinvocation implements methodinvocation {
 
  private final object targetobject;
 
  private final method targetmethod;
 
  private final list<methodinterceptor> interceptorlist;
 
  private int currentinterceptorindex = -1;
 
  public reflectivemethodinvocation(object targetobject, method targetmethod, list<methodinterceptor> interceptorlist) {
    this.targetobject = targetobject;
    this.targetmethod = targetmethod;
    this.interceptorlist = interceptorlist;
  }
 
  @override
  public object proceed() throws throwable {
 
    if (this.currentinterceptorindex == this.interceptorlist.size() - 1) {
      return invokejoinpoint();
    }
 
    this.currentinterceptorindex++;
 
    methodinterceptor interceptor =
        this.interceptorlist.get(this.currentinterceptorindex);
    return interceptor.invoke(this);
  }
 
  private object invokejoinpoint() throws throwable {
 
    return this.targetmethod.invoke(this.targetobject);
  }
}

niocoderservice

模擬service

?
1
2
3
4
5
6
public class niocoderservice {
 
  public void testaop() {
    system.out.println("http://niocoder.com/");
  }
}

transactionmanager

模擬通知類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class transactionmanager {
  public void start() {
    system.out.println("start tx");
  }
 
  public void commit() {
    system.out.println("commit tx");
  }
 
  public void rollback() {
    system.out.println("rollback tx");
  }
 
}

reflectivemethodinvocationtest

beforeadvice->afterreturningadvice

測試類,測試通知

?
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
public class reflectivemethodinvocationtest {
 
  private aspectjbeforeadvice beforeadvice = null;
 
  private aspectjafterreturningadvice afterreturningadvice = null;
 
  private niocoderservice niocoderservice;
 
  private transactionmanager tx;
 
  public void setup() throws exception {
    niocoderservice = new niocoderservice();
    tx = new transactionmanager();
    beforeadvice = new aspectjbeforeadvice(transactionmanager.class.getmethod("start"), tx);
    afterreturningadvice = new aspectjafterreturningadvice(transactionmanager.class.getmethod("commit"), tx);
  }
 
  public void testmethodinvocation() throws throwable {
    method method = niocoderservice.class.getmethod("testaop");
    list<methodinterceptor> interceptorlist = new arraylist<>();
    interceptorlist.add(beforeadvice);
    interceptorlist.add(afterreturningadvice);   
 
    reflectivemethodinvocation mi = new reflectivemethodinvocation(niocoderservice, method, interceptorlist);
 
    mi.proceed();
  }
 
 
  public static void main(string[] args) throws throwable {
    reflectivemethodinvocationtest reflectivemethodinvocationtest = new reflectivemethodinvocationtest();
    reflectivemethodinvocationtest.setup();
    reflectivemethodinvocationtest.testmethodinvocation();
  }
}

輸出:

start tx
http://niocoder.com/
commit tx

時序圖 beforeadvice->afterreturningadvice

spring aop之鏈式調用的實現

spring aop之鏈式調用的實現

afterreturningadvice->beforeadvice

修改interceptorlist的順序

?
1
2
3
4
5
6
7
8
9
10
public void testmethodinvocation() throws throwable {
    method method = niocoderservice.class.getmethod("testaop");
    list<methodinterceptor> interceptorlist = new arraylist<>();
    interceptorlist.add(afterreturningadvice);
     interceptorlist.add(beforeadvice);
 
    reflectivemethodinvocation mi = new reflectivemethodinvocation(niocoderservice, method, interceptorlist);
 
    mi.proceed();
  }

輸出:

start tx
http://niocoder.com/
commit tx

時序圖 afterreturningadvice->beforeadvice

spring aop之鏈式調用的實現

spring aop之鏈式調用的實現

代碼下載

github:https://github.com/longfeizheng/data-structure-java/blob/master/src/main/java/cn/merryyou/aop

代碼下載

github:https://github.com/longfeizheng/data-structure-java

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

原文鏈接:https://segmentfault.com/a/1190000018206756

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲福利二区 | 168黄网| 国产精品18久久久 | 精品视频在线一区 | 亚洲精品久久久久久下一站 | 日本三级视频在线观看 | 久久国产精品99久久久久久老狼 | 二区影院 | 九色91| 91精品久久久久久久 | 亚洲在线一区 | 尤物在线观看网站 | 亚洲精品久久久久久久久久久 | 日韩精品久久久 | 国产高清自拍视频 | 免费看国产一级片 | 免费黄色小视频 | 国产成人精品一区二区三区四区 | 亚洲电影在线 | 美女视频一区二区三区 | 欧美福利一区 | 成年免费视频 | 天堂av中文字幕 | 欧美aaa级 | 国产精品久久久久免费a∨ 欧美黄色精品 | 日韩中文字幕在线观看 | 日本精品久久 | 久久精品国产77777蜜臀 | 久久国产乱 | www.日韩 | 91社影院在线观看 | 一级片黄色大片 | 久久精品一区 | 不卡黄色片| 黄色资源网站 | 91最新网站 | 精品久久网 | 久久女人精品 | 亚洲精品一区二区 | 午夜小电影 | 国产精品久久久久久久久久妞妞 |