關關雎鳩,在河之洲。窈窕淑女,君子好逑。

aop
(aspect orient programming
),我們一般稱為面向方面(切面)程式設計,作為物件導向的一種補充,用於處理系統中分布於各個模組的橫切關注點,比如事務管理、日誌、快取等等。spring
aop
採用的是動態**,在執行期間對業務方法進行增強,所以不會生成新類,spring
aop
提供了對jdk
動態**的支援以及cglib的支援。本章我們不關注aop
**類的實現,我簡單實現乙個指定次序的鏈式呼叫。
抽象類,實現methodinterceptor
aspectjbeforeadvicepublic abstract class abstractaspectjadvice implements methodinterceptor
public method getadvicemethod()
public void invokeadvicemethod() throws throwable
}
前置通知
aspectjafterreturningadvicepublic class aspectjbeforeadvice extends abstractaspectjadvice
@override
public object invoke(methodinvocation invocation) throws throwable
}
後置通知
reflectivemethodinvocationpublic class aspectjafterreturningadvice extends abstractaspectjadvice
@override
public object invoke(methodinvocation invocation) throws throwable
}
實現methodinvocation
,proceed()
方法遞迴實現鏈式呼叫。
niocoderservicepublic class reflectivemethodinvocation implements methodinvocation
@override
public object proceed() throws throwable
this.currentinterceptorindex++;
methodinterceptor interceptor =
this.interceptorlist.get(this.currentinterceptorindex);
return interceptor.invoke(this);
}private object invokejoinpoint() throws throwable
}
模擬service
類
transactionmanagerpublic class niocoderservice
}
模擬通知類
reflectivemethodinvocationtestpublic class transactionmanager
public void commit()
public void rollback()
}
beforeadvice->afterreturningadvice
測試類,測試通知
輸出:public class reflectivemethodinvocationtest
public void testmethodinvocation() throws throwable
public static void main(string args) throws throwable
}
時序圖 beforeadvice->afterreturningadvicestart tx
commit tx
修改interceptorlist
的順序
public void testmethodinvocation() throws throwable
輸出:
時序圖 afterreturningadvice->beforeadvicestart tx
commit tx
spring aop 之鏈式呼叫
關關雎鳩,在河之洲。窈窕淑女,君子好逑。aop aspect orient programming 我們一般稱為面向方面 切面 程式設計,作為物件導向的一種補充,用於處理系統中分布於各個模組的橫切關注點,比如事務管理 日誌 快取等等。springaop採用的是動態 在執行期間對業務方法進行增強,所以...
spring aop 之鏈式呼叫
關關雎鳩,在河之洲。窈窕淑女,君子好逑。aop aspect orient programming 我們一般稱為面向方面 切面 程式設計,作為物件導向的一種補充,用於處理系統中分布於各個模組的橫切關注點,比如事務管理 日誌 快取等等。springaop採用的是動態 在執行期間對業務方法進行增強,所以...
Swift學習之可選鏈式呼叫
一種在當前值可能為nil的可選值上呼叫屬性 方法 下標的方法,如果可選值有值,則呼叫成功,否則就返回nil。多個呼叫可以連線在一起形成一條鏈,如果整條鏈的某個節點為nil,則呼叫不成功,返回nil。class person class residence subscript index int ro...