1. 啟用aop
a. 在類上添加@aspect注解
b. 注入該類, 可以使用@component進行注入到spring容器中
2. 通過pointcut對象創建切入點
a. 在某個方法使用類似下面的方法進行注入
1
2
3
|
@pointcut ( "execution(* com.sguess.service.iaopservice.*(..))" ) private void pointcut() { } |
i. 其中,execution表達式為
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
ii. 注意, pointcut()方法名是后面切入的時候需要使用的
iii. 方法內可以什么也不寫, 寫了也調不到
iv. 也可以創建多個pointcut,例如再創建一個
1
2
3
|
@pointcut ( "execution(* com.sguess.service.iaopservice.fun1(..))" ) private void pointcut2() { } |
這個的方法名就位pointcut2, 方法名不一樣.
b. 創建after方法,before方法
1
2
3
4
|
@after (value = "pointcut()" ) public void doafter() { system.out.println( "do aop after function 01" ); } |
i. after方法是指, 在配置了的切入點被執行后, 執行該方法.
ii. value中的pointcut() 是我們前面在創建@pointcut中的方法名. 也就是說,是通過方法名和切入點進行匹配的.
iii. 這個的方法名可以隨便起.
iv. before方法同理
c. 帶return的after方法,
1
2
3
4
|
@afterreturning (returning = "str" , pointcut = "pointcut()" ) public void doafterreturning(string str) throws exception { system.out.println( "return value is: " + str); } |
i. afterreturn是指在被切入的方法執行后, 獲取其返回值, 再執行該方法. 注意關鍵, 這個可以進行操作返回值.
ii. returning = "str",是指, 假設切入方法的返回的值變量名為str
doafterreturning(string str)方法的參數變量名必須和和returning保持一致, 這里也叫作str. 然后才能在方法體中使用.
iii. pointcut = "pointcut()"同樣是指前面聲明的pointcut方法名
3. 通過注解, 使用切入點
a. 監聽方法參數
1
2
3
4
5
6
7
8
9
10
11
12
|
@before ( "execution(public int com.sguess.service.*(int, int))" ) public void beformethod(joinpoint point) { string methodname = point.getsignature().getname(); list<object> args = arrays.aslist(point.getargs()); system.out.println( "before functionname:" + methodname + ",parametername:" + args); } @after ( "execution(public int com.sguess.service.*(int, int))" ) public void aftermethod(joinpoint point) { string methodname = point.getsignature().getname(); list<object> args = arrays.aslist(point.getargs()); system.out.println( "after functionname:" + methodname + ",parametername:" + args); } |
4. 執行順序:
a.around的方法優先于before/after執行,after優先于afterreturn.
i. 代碼
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
|
@before ( "execution(public int com.sguess.service.*.*(int, int))" ) public void beformethod(joinpoint point) { system.out.println( "before function" ); } @after ( "execution(public int com.sguess.service.*.*(int, int))" ) public void aftermethod(joinpoint point) { system.out.println( "after function" ); } @afterreturning ( "execution(public int com.sguess.service.*.*(int, int))" ) public void afterreturnmethod(joinpoint point) { system.out.println( "afterreturn function" ); } @afterthrowing (value = "execution(public int com.sguess.service.*.*(int, int))" , throwing = "e" ) public void afterreturningthrowing(joinpoint point, exception e) { system.out.println( "afterreturnthrowing function" ); } @around ( "execution(public int com.sguess.service.*.*(int, int))" ) public object aroundmethod(proceedingjoinpoint pdj) { system.out.println( "start aroundfunction" ); object result = null ; try { system.out.println( "around process start" ); result = pdj.proceed(); system.out.println( "around process end" ); } catch (throwable e) { system.out.println( "around process exception" ); } system.out.println( "after around process" ); return result; } } |
執行結果:
start aroundfunction
around process start
before function
around process end
after around process
after function
afterreturn function
5.小結:
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
|
@afterreturning (returning = "str" , pointcut = "pointcut()" ) public void doafterreturning(string str) throws exception { system.out.println( "return value is: " + str); } @before ( "execution(public int com.sguess.service.*.*(int, int))" ) public void beformethod(joinpoint point) { string methodname = point.getsignature().getname(); list<object> args = arrays.aslist(point.getargs()); system.out.println( "before functionname:" + methodname + ",parametername:" + args); } @after ( "execution(public int com.sguess.service.*.*(int, int))" ) public void aftermethod(joinpoint point) { string methodname = point.getsignature().getname(); list<object> args = arrays.aslist(point.getargs()); system.out.println( "after functionname:" + methodname + ",parametername:" + args); } @afterthrowing (value = "execution(public int com.sguess.service.*.*(int, int))" , throwing = "e" ) public void afterreturningthrowing(joinpoint point, exception e) { string methodname = point.getsignature().getname(); list<object> args = arrays.aslist(point.getargs()); system.out.println( "afterreturningthrowing functionname:" + methodname + ",parametername:" + args + ",exception:" + e); } @around ( "execution(public int com.sguess.service.*.*(int, int))" ) public object aroundmethod(proceedingjoinpoint pdj) { system.out.println( "start aroundfunction" ); object result = null ; try { system.out.println( "around process start" ); result = pdj.proceed(); system.out.println( "around process end" ); } catch (throwable e) { system.out.println( "around process exception" ); } system.out.println( "after around process" ); return result; } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/sanpic/article/details/82800017