spring框架應用系列四:切面編程(環繞通知與前後置通知區別)

                           切面編程(環繞通知與前後置通知區別)                           

                                              本文系作者原創,轉載請註明出處:http://www.cnblogs.com/further-further-further/p/7867034.html

解決問題

1、擁有前置通知和後置通知的功能,並能解決前置通知和後置通知在共享信息方面的不足(例如:統計切點方法執行時間);

2、在多線程併發條件下,能保證線程安全(因爲在一個方法內定義的局部變量);

3、解決代碼重複性,降低代碼複雜程度;

內容說明

1、以下會給出前置通知、後置通知與環繞通知實例(觀衆觀看錶演),通過對比更能理解彼此之間的區別;

2、兩者都通過@Component註解,掃描(Audience,Juggler)bean並註冊到spring容器中時,需在XML配置文件中引入component-scan(前後置通知:<context:component-scan base-package="com.spring.example.aspectAspectJNoArgs"/> 環繞通知:<context:component-scan base-package="com.spring.example.aspectAround"/>)

3、切面是觀衆(Audience),切點是節目表演(Performance.perform())
       前置通知:在節目表演之前,觀衆就坐(調用Audience的takeSeats方法),並關掉手機(調用Audience的turnOffCellPhones方法);
       後置通知:在節目表演結束,觀衆鼓掌(調用Audience的applaud方法);
       異常通知:節目表演出現異常,觀衆要求退票(調用Audience的demandRefund方法);

環繞通知:其他與上面相同,只是在節目表演開始與結束時打印時間,統計節目表演時長;

4、通過執行Juggler的perform方法,從而執行切面Audience中相應的方法,達到通知的效果;

應用實例:觀衆觀看錶演所做出的相應行爲

先列出相關接口以及類代碼 

節目表演接口(切點方法)

 

1 package com.spring.example.aspectAround;
2 
3 /**
4  * Created by weixw on 2017/11/16.
5  */
6 public interface Performer {
7 
8     void perform();
9 }

切點類實現接口Juggler

 

 1 package com.spring.example.aspectAround;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6  * Created by weixw on 2017/11/16.
 7  */
 8 @Component
 9 public class Juggler implements Performer {
10     private int beanBags = 3;
11     public Juggler(){
12 
13     }
14     public Juggler(int beanBags){
15         this.beanBags = beanBags ;
16     }
17     @Override
18     public void perform()  {
19         System.out.println("JUGGLING "+ beanBags + " BEANBAGS");
20         try {
21             Thread.sleep(1);
22         }catch (InterruptedException e){
23             e.printStackTrace();
24         }
25     }
26 
27 
28 }

上述代碼都能共用,下面分別列舉前後置通知與環繞通知區別代碼

前後置通知(通過AspectJ註解實現,注意:<aop:aspectj-autoproxy/>不能少,它實現了切面相關方法綁定在切點上,切點方法執行就能觸發相應通知)

XML配置文件:spring/aspect-aspectJnoArgs.xml(放在spring文件夾下)

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 7 
 8     <!--使用前置通知和後置通知唯一方式:在前置通知中記錄開始時間,並在後置通知中報告表演耗費的時長,必須保存開始時間。因爲Audience是單例,如果像這樣保-->
 9     <!--存狀態,會存在線程安全問題;-->
10     <context:component-scan base-package="com.spring.example.aspectAspectJNoArgs"/>
11     <aop:aspectj-autoproxy/>
12 </beans>
View Code

前後置通知切面實現類

 

 1 package com.spring.example.aspectAspectJNoArgs;
 2 
 3 import org.aspectj.lang.annotation.*;
 4 import org.springframework.stereotype.Component;
 5 
 6 /**
 7  * Created by weixw on 2017/11/16.
 8  * 通過AspectJ註解實現切面編程
 9  * 切點方法 id 默認是所依賴方法(public void performance(){})的小寫方法名performance
10  */
11 
12 @Component
13 @Aspect
14 public class Audience {
15     @Pointcut("execution(* com.spring.example.aspectAspectJNoArgs.Performer.perform(..))") //定義切點
16     public void performance(){}
17     @Before("performance()")//表演之前
18     public void takeSeats(){
19         System.out.println("The audience is taking their seats.");
20     }
21     @Before("performance()")//表演之前
22     public void turnOffCellPhones(){
23         System.out.println("The audience is turning off their cellphones.");
24     }
25     @AfterReturning("performance()")//表演之後
26     public void applaud(){
27         System.out.println("CLAP CLAP CLAP CLAP CLAP ");
28     }
29     @AfterThrowing("performance()") //表演失敗之後
30     public void demandRefund(){
31         System.out.println("Boo! We want our money back!");
32     }
33 }
View Code

 

環繞通知

XML配置文件:spring/aspect-around.xml(放在spring文件夾下)

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 7 
 8     <!--前置通知和後置通知是在一個方法中實現,所以不需要保存變量值,自然是線程安全的;-->
 9 
10     <context:component-scan base-package="com.spring.example.aspectAround"/>
11     <!--通過component-scan自動掃描,@Component註解將Magician註冊到spring容器-->
12     <aop:config>
13             <!--audience :切面  watchPerformance:切面方法   performance:切點-->
14             <aop:aspect ref="audience">
15                 <aop:pointcut id="performance" expression="execution(* com.spring.example.aspectAround.Performer.perform(..))"/>
16                 <aop:around pointcut-ref="performance" method="watchPerformance" />
17             </aop:aspect>
18     </aop:config>
19 </beans>
View Code

 

環繞通知切面實現類

 

 1 package com.spring.example.aspectAround;
 2 
 3 import org.aspectj.lang.ProceedingJoinPoint;
 4 import org.springframework.stereotype.Component;
 5 
 6 /**
 7  * Created by weixw on 2017/11/16.
 8  */
 9 @Component
10 public class Audience {
11     public void takeSeats(){
12         System.out.println("The audience is taking their seats.");
13     }
14     public void turnOffCellPhones(){
15         System.out.println("The audience is turning off their cellphones.");
16     }
17     public void applaud(){
18         System.out.println("CLAP CLAP CLAP CLAP CLAP");
19     }
20     public void demandRefund(){
21         System.out.println("Boo! We want our money back!");
22     }
23 
24     public void watchPerformance(ProceedingJoinPoint joinPoint){
25         try{
26             takeSeats(); //表演之前
27             turnOffCellPhones(); //表演之前
28             long start = System.currentTimeMillis();
29             System.out.println("The performance start ......");//節目開始
30             joinPoint.proceed(); //執行被通知的方法
31             System.out.println("The performance end ......");//節目結束
32             long end = System.currentTimeMillis(); //表演之後
33             applaud();//表演之後
34             System.out.println("The performance took milliseconds:"+ (end - start) );//表演時長
35         }catch (Throwable t){
36             demandRefund(); //表演失敗之後
37         }
38     }
39 }
View Code

 

測試代碼

環繞通知測試代碼如下,前後置通知測試代碼只需將配置文件名稱改成spring/aspect-aspectJnoArgs.xml即可

 

 1 package com.spring.example.aspectAround;/**
 2  * Created by weixw on 2017/11/16.
 3  */
 4 
 5 import javafx.application.Application;
 6 import javafx.stage.Stage;
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 public class Driver extends Application {
11 
12     public static void main(String[] args) {
13         launch(args);
14     }
15 
16     @Override
17     public void start(Stage primaryStage) {
18         try {
19 
20 
21             ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/aspect-around.xml");
22             Performer performer = (Performer) ctx.getBean("juggler");
23             performer.perform();
24 
25         }catch (Exception e){
26             e.printStackTrace();
27         }
28     }
29 }

 

 

 

運行結果

環繞通知結果:

 

前後置通知結果:

  

總結

上述列出前後置通知和環繞通知樣例。對於有變量緩存需求,線程安全的應用場景,前後置通知實現比較困難,而環繞通知實現就非常容易;

 

 

 不要讓懶惰佔據你的大腦,不要讓妥協拖垮你的人生。青春就是一張票,能不能趕上時代的快車,你的步伐掌握在你的腳下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章