Udemy Course Spring Framework 35-Spring AOP!

ZONGRU Li
3 min readMar 15, 2019

--

自製AOP annotation(可以在想"切入"的地方放上annotation)並運用!

i.e.做一個annotation標籤,在要執行的程式放上標籤

建一個叫TrackTime的class

並將class改為@interface如下格式:

實際製做成如下設定:

再來改寫原本的Dao1,原本設定如下:

將Dao1改寫為:

然後我們依然用上次寫到的,將這個切入定義在CommonJoinPointConfig內

原本的MethodExecutionCalculationAspect程式如下

使用businessLayerExecution這個Pointcut

現在也改為:

最後再次執行未異動的SpringAopApplication.java得到:

解釋:

我們在MethodExecutionCalculationAspect.java內

有定義@Around去算程式執行的時間,並定義名為around Method

而要使用的around Method的程式又統一定義在CommonJoinPointConfig

其中的trackTimeAnnotation Method

原本@Pointcut後面括號內是指定要取用"切入"的程式所在位置

現在@Pointcut後面括號內是指定標籤-@TrackTime的java檔

同理在business1的Method內也加一個annotation

再次執行SpringAopApplication.java

想在哪一個Method去看執行時間!?

現在只要加個@TrackTime在那Method上頭就好!!

--

--

ZONGRU Li
ZONGRU Li

Written by ZONGRU Li

2022/11/17 開源部分個人筆記給LINE "Java程式語言討論區"社群,希望能對社群的技術學習做一點點貢獻.(掩面....記得退訂閱!

No responses yet