前置通知[before advice]:在連接點前面執行,前置通知不會影響連接點的執行,除非此處拋出異常。
正常返回通知[after returning advice]:在連接點正常執行完成后執行,如果連接點拋出異常,則不會執行。
異常返回通知[after throwing advice]:在連接點拋出異常后執行。
返回通知[after (finally) advice]:在連接點執行完成后執行,不管是正常執行完成,還是拋出異常,都會執行返回通知中的內容。
環繞通知[around advice]:環繞通知圍繞在連接點前后,比如一個方法調用的前后。這是最強大的通知類型,能在方法調用前后自定義一些操作。
環繞通知還需要負責決定是繼續處理join point(調用proceedingjoinpoint的proceed方法)還是中斷執行。
接下來通過編寫示例程序來測試一下五種通知類型:
定義接口
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.chenqa.springaop.example.service; public interface bankservice { /** * 模擬的銀行轉賬 * @param from 出賬人 * @param to 入賬人 * @param account 轉賬金額 * @return */ public boolean transfer(string form, string to, double account); } |
編寫實現類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.chenqa.springaop.example.service.impl; import com.chenqa.springaop.example.service.bankservice; public class bcmbankserviceimpl implements bankservice { public boolean transfer(string form, string to, double account) { if (account< 100 ) { throw new illegalargumentexception( "最低轉賬金額不能低于100元" ); } system.out.println(form+ "向" +to+ "交行賬戶轉賬" +account+ "元" ); return false ; } } |
修改spring配置文件,添加以下內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- bankservice bean --> <bean id= "bankservice" class = "com.chenqa.springaop.example.service.impl.bcmbankserviceimpl" /> <!-- 切面 --> <bean id= "myaspect" class = "com.chenqa.springaop.example.aspect.myaspect" /> <!-- aop配置 --> <aop:config> <aop:aspect ref= "myaspect" > <aop:pointcut expression= "execution(* com.chenqa.springaop.example.service.impl.*.*(..))" id= "pointcut" /> <aop:before method= "before" pointcut-ref= "pointcut" /> <aop:after method= "after" pointcut-ref= "pointcut" /> <aop:after-returning method= "afterreturning" pointcut-ref= "pointcut" /> <aop:after-throwing method= "afterthrowing" pointcut-ref= "pointcut" /> <aop:around method= "around" pointcut-ref= "pointcut" /> </aop:aspect> </aop:config> |
編寫測試程序
1
2
3
|
applicationcontext context = new classpathxmlapplicationcontext( "spring-aop.xml" ); bankservice bankservice = context.getbean( "bankservice" , bankservice. class ); bankservice.transfer( "張三" , "李四" , 200 ); |
執行后輸出:
將測試程序中的200改成50,再執行后輸出:
通過測試結果可以看出,五種通知的執行順序為:
前置通知→環繞通知→正常返回通知/異常返回通知→返回通知,可以多次執行來查看。
情況一: 一個方法只被一個aspect類攔截
當一個方法只被一個aspect攔截時,這個aspect中的不同advice是按照怎樣的順序進行執行的呢?請看:
添加 pointcut類
該pointcut用來攔截test包下的所有類中的所有方法。
1
2
3
4
5
6
7
8
9
10
|
package test; import org.aspectj.lang.annotation.pointcut; public class pointcuts { @pointcut (value = "within(test.*)" ) public void aopdemo() { } } |
添加aspect類
該類中的advice將會用到上面的pointcut,使用方法請看各個advice的value屬性。
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
37
38
|
package test; import org.aspectj.lang.joinpoint; import org.aspectj.lang.proceedingjoinpoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.component; @component @aspect public class aspect1 { @before (value = "test.pointcuts.aopdemo()" ) public void before(joinpoint joinpoint) { system.out.println( "[aspect1] before advise" ); } @around (value = "test.pointcuts.aopdemo()" ) public void around(proceedingjoinpoint pjp) throws throwable{ system.out.println( "[aspect1] around advise 1" ); pjp.proceed(); system.out.println( "[aspect1] around advise2" ); } @afterreturning (value = "test.pointcuts.aopdemo()" ) public void afterreturning(joinpoint joinpoint) { system.out.println( "[aspect1] afterreturning advise" ); } @afterthrowing (value = "test.pointcuts.aopdemo()" ) public void afterthrowing(joinpoint joinpoint) { system.out.println( "[aspect1] afterthrowing advise" ); } @after (value = "test.pointcuts.aopdemo()" ) public void after(joinpoint joinpoint) { system.out.println( "[aspect1] after advise" ); } } |
添加測試用controller
添加一個用于測試的controller,這個controller中只有一個方法,但是它會根據參數值的不同,會作出不同的處理:一種是正常返回一個對象,一種是拋出異常(因為我們要測試@afterthrowing這個advice)
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
37
38
39
40
41
42
43
44
45
46
47
48
|
package test; import test.exception.testexception; import org.springframework.http.httpstatus; import org.springframework.web.bind.annotation.*; @restcontroller @requestmapping (value = "/aop" ) public class aoptestcontroller { @responsestatus (httpstatus.ok) @requestmapping (value = "/test" , method = requestmethod.get) public result test( @requestparam boolean throwexception) { // case 1 if (throwexception) { system.out.println( "throw an exception" ); throw new testexception( "mock a server exception" ); } // case 2 system.out.println( "test ok" ); return new result() {{ this .setid( 111 ); this .setname( "mock a result" ); }}; } public static class result { private int id; private string name; public int getid() { return id; } public void setid( int id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } } } |
測試 正常情況
在瀏覽器直接輸入以下的url,回車:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=false
1
我們會看到輸出的結果是:
1
2
3
4
5
6
|
[aspect1] around advise 1 [aspect1] before advise test ok [aspect1] around advise2 [aspect1] after advise [aspect1] afterreturning advise |
測試 異常情況
在瀏覽器中直接輸入以下的url,回車:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=true
1
我們會看到輸出的結果是:
1
2
3
4
5
|
[aspect1] around advise 1 [aspect1] before advise throw an exception [aspect1] after advise [aspect1] afterthrowing advise |
結論
在一個方法只被一個aspect類攔截時,aspect類內部的 advice 將按照以下的順序進行執行:
正常情況:
異常情況:
情況二: 同一個方法被多個aspect類攔截
此處舉例為被兩個aspect類攔截。
有些情況下,對于兩個不同的aspect類,不管它們的advice使用的是同一個pointcut,還是不同的pointcut,都有可能導致同一個方法被多個aspect類攔截。那么,在這種情況下,這多個aspect類中的advice又是按照怎樣的順序進行執行的呢?請看:
pointcut類保持不變
添加一個新的aspect類
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
37
38
|
package test; import org.aspectj.lang.joinpoint; import org.aspectj.lang.proceedingjoinpoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.component; @component @aspect public class aspect2 { @before (value = "test.pointcuts.aopdemo()" ) public void before(joinpoint joinpoint) { system.out.println( "[aspect2] before advise" ); } @around (value = "test.pointcuts.aopdemo()" ) public void around(proceedingjoinpoint pjp) throws throwable{ system.out.println( "[aspect2] around advise 1" ); pjp.proceed(); system.out.println( "[aspect2] around advise2" ); } @afterreturning (value = "test.pointcuts.aopdemo()" ) public void afterreturning(joinpoint joinpoint) { system.out.println( "[aspect2] afterreturning advise" ); } @afterthrowing (value = "test.pointcuts.aopdemo()" ) public void afterthrowing(joinpoint joinpoint) { system.out.println( "[aspect2] afterthrowing advise" ); } @after (value = "test.pointcuts.aopdemo()" ) public void after(joinpoint joinpoint) { system.out.println( "[aspect2] after advise" ); } } |
測試用controller也不變
還是使用上面的那個controller。但是現在 aspect1 和 aspect2 都會攔截該controller中的方法。
下面繼續進行測試!
測試 正常情況
在瀏覽器直接輸入以下的url,回車:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=false
1
我們會看到輸出的結果是:
1
2
3
4
5
6
7
8
9
10
11
|
[aspect2] around advise 1 [aspect2] before advise [aspect1] around advise 1 [aspect1] before advise test ok [aspect1] around advise2 [aspect1] after advise [aspect1] afterreturning advise [aspect2] around advise2 [aspect2] after advise [aspect2] afterreturning advise |
但是這個時候,我不能下定論說 aspect2 肯定就比 aspect1 先執行。
不信?你把服務務器重新啟動一下,再試試,說不定你就會看到如下的執行結果:
1
2
3
4
5
6
7
8
9
10
11
|
[aspect1] around advise 1 [aspect1] before advise [aspect2] around advise 1 [aspect2] before advise test ok [aspect2] around advise2 [aspect2] after advise [aspect2] afterreturning advise [aspect1] around advise2 [aspect1] after advise [aspect1] afterreturning advise |
也就是說,這種情況下, aspect1 和 aspect2 的執行順序是未知的。那怎么解決呢?不急,下面會給出解決方案。
測試 異常情況
在瀏覽器中直接輸入以下的url,回車:http://192.168.142.8:7070/aoptest/v1/aop/test?throwexception=true
1
我們會看到輸出的結果是:
1
2
3
4
5
6
7
8
9
|
[aspect2] around advise 1 [aspect2] before advise [aspect1] around advise 1 [aspect1] before advise throw an exception [aspect1] after advise [aspect1] afterthrowing advise [aspect2] after advise [aspect2] afterthrowing advise |
同樣地,如果把服務器重啟,然后再測試的話,就可能會看到如下的結果:
1
2
3
4
5
6
7
8
9
|
[aspect1] around advise 1 [aspect1] before advise [aspect2] around advise 1 [aspect2] before advise throw an exception [aspect2] after advise [aspect2] afterthrowing advise [aspect1] after advise [aspect1] afterthrowing advise |
也就是說,同樣地,異常情況下, aspect1 和 aspect2 的執行順序也是未定的。
那么在 情況二 下,如何指定每個 aspect 的執行順序呢?
方法有兩種:
- 實現org.springframework.core.ordered接口,實現它的getorder()方法
- 給aspect添加@order注解,該注解全稱為:org.springframework.core.annotation.order
不管采用上面的哪種方法,都是值越小的 aspect 越先執行。
比如,我們為 apsect1 和 aspect2 分別添加 @order 注解,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@order ( 5 ) @component @aspect public class aspect1 { // ... } @order ( 6 ) @component @aspect public class aspect2 { // ... } |
這樣修改之后,可保證不管在任何情況下, aspect1 中的 advice 總是比 aspect2 中的 advice 先執行。如下圖所示:
注意點
如果在同一個 aspect 類中,針對同一個 pointcut,定義了兩個相同的 advice(比如,定義了兩個 @before),那么這兩個 advice 的執行順序是無法確定的,哪怕你給這兩個 advice 添加了 @order 這個注解,也不行。這點切記。
對于@around這個advice,不管它有沒有返回值,但是必須要方法內部,調用一下 pjp.proceed();否則,controller 中的接口將沒有機會被執行,從而也導致了 @before這個advice不會被觸發。比如,我們假設正常情況下,執行順序為”aspect2 -> apsect1 -> controller”,如果,我們把 aspect1中的@around中的 pjp.proceed();給刪掉,那么,我們看到的輸出結果將是:
1
2
3
4
5
6
7
8
9
|
[aspect2] around advise 1 [aspect2] before advise [aspect1] around advise 1 [aspect1] around advise2 [aspect1] after advise [aspect1] afterreturning advise [aspect2] around advise2 [aspect2] after advise [aspect2] afterreturning advise |
從結果可以發現, controller 中的 接口 未被執行,aspect1 中的 @before advice 也未被執行。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/qq_35873847/article/details/78624941