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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

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

這篇文章主要介紹了spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

概述

aopaspect orient programming),我們一般稱為面向方面(切面)編程,作為面向?qū)ο蟮囊环N補(bǔ)充,用于處理系統(tǒng)中分布于各個(gè)模塊的橫切關(guān)注點(diǎn),比如事務(wù)管理、日志、緩存等等。 spring aop采用的是動(dòng)態(tài)代理,在運(yùn)行期間對(duì)業(yè)務(wù)方法進(jìn)行增強(qiáng),所以不會(huì)生成新類,spring aop提供了對(duì)jdk動(dòng)態(tài)代理的支持以及cglib的支持。本章我們不關(guān)注aop代理類的實(shí)現(xiàn),我簡(jiǎn)單實(shí)現(xiàn)一個(gè)指定次序的鏈?zhǔn)秸{(diào)用

實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用的

methodinterceptor定義攔截器鏈,methodinvocation 遞歸進(jìn)入下一個(gè)攔截器鏈中。類圖如下:

spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

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

抽象類,實(shí)現(xiàn)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

實(shí)現(xiàn)methodinvocationproceed()方法遞歸實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用。

?
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

測(cè)試類,測(cè)試通知

?
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

時(shí)序圖 beforeadvice->afterreturningadvice

spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

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

時(shí)序圖 afterreturningadvice->beforeadvice

spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

spring aop之鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

代碼下載

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

代碼下載

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女机机对机机的视频(免费) | 午夜理论片日本中文在线 | 欧美日韩精品一区二区三区视频播放 | 亚洲另类老妇videos | 草草影院在线 | 日本哺乳期网站xxxx | bt天堂在线最新版www | 99re8在这里只有精品2 | 欧美一区二区日韩一区二区 | 亚洲香蕉网久久综合影院3p | 白丝校花掀起短裙呻吟小说 | 日韩一区二区三区四区五区 | 国产午夜精品一区二区三区 | 国产精品区牛牛影院 | 国产日韩一区二区 | 嫩草蜜桃 | 91综合在线视频 | 丝瓜茄子绿巨人秋葵榴莲污 | 国产激情视频网站 | 日韩免费在线视频 | 天天视频官网天天视频在线 | 欧美日韩不卡视频 | 99视频福利 | 动漫美女人物被黄漫小说 | 91四虎国自产在线播放线 | 欧美3d怪物交videos网站 | 无码乱人伦一区二区亚洲 | 亚洲网红精品大秀在线观看 | caoporn草棚在线视频 | 欧美猛男同志同性video | 欧美亚洲国产另类在线观看 | 精品久久久久亚洲 | 双性少爷受糙汉攻h | 日本人成动漫网站在线观看 | 国产精品区牛牛影院 | 美女撒尿毛片免费看 | 久久免费看少妇高潮A片特爽 | 日韩亚洲人成在线综合 | 免费国产在线观看 | 高清国产激情视频在线观看 | 欧美精品一区二区三区免费播放 |