3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
listenzhangbin.com/post/2016/09/spring-aop-cglib/AOP(Aspect Orient Programming),我們一般稱為面向方面(切面)編程,作為面向對象的一種補充,用於處理系統中分佈於各個模塊的橫切關注點,比如事務管理、日誌、緩存等等。AOP實現的關鍵在於AOP框架自動創建的AOP代理,AOP代理主要分為靜態代理和動態代理,靜態代理的代表為AspectJ;而動態代理則以Spring AOP為代表。本文會分別對AspectJ和Spring AOP的實現進行分析和介紹。使用AspectJ的編譯時增強實現AOP之前提到,AspectJ是靜態代理的增強,所謂的靜態代理就是AOP框架會在編譯階段生成AOP代理類,因此也稱為編譯時增強。舉個實例的例子來說。首先我們有一個普通的Hello類使用AspectJ編寫一個Aspectpublic aspect TxAspect { void around:call(void Hello.sayHello){ System.out.println("開始事務 ..."); proceed; System.out.println("事務結束 ..."); }}這裡模擬了一個事務的場景,類似於Spring的聲明式事務。使用AspectJ的編譯器編譯編譯完成之後再運行這個Hello類,可以看到以下輸出開始事務 ...hello事務結束 ...顯然,AOP已經生效了,那麼究竟AspectJ是如何在沒有修改Hello類的情況下為Hello類增加新功能的呢?查看一下編譯后的Hello.classpublic class Hello { public Hello { } public void sayHello { System.out.println("hello"); } public static void main(String args) { Hello h = new Hello; sayHello_aroundBody1$advice(h, TxAspect.aspectOf, (AroundClosure)null); }}可以看到,這個類比原來的Hello.java多了一些代碼,這就是AspectJ的靜態代理,它會在編譯階段將Aspect織入Java位元組碼中, 運行的時候就是經過增強之後的AOP對象。public void ajc$around$com_listenzhangbin_aop_TxAspect$1$f54fe983(AroundClosure ajc$aroundClosure) { System.out.println("開始事務 ..."); ajc$around$com_listenzhangbin_aop_TxAspect$1$f54fe983proceed(ajc$aroundClosure); System.out.println("事務結束 ..."); }從Aspect編譯后的class文件可以更明顯的看出執行的邏輯。proceed方法就是回調執行被代理類中的方法。使用Spring AOP與AspectJ的靜態代理不同,Spring AOP使用的動態代理,所謂的動態代理就是說AOP框架不會去修改位元組碼,而是在內存中臨時為方法生成一個AOP對象,這個AOP對象包含了目標對象的全部方法,並且在特定的切點做了增強處理,並回調原對象的方法。Spring AOP中的動態代理主要有兩種方式,JDK動態代理和CGLIB動態代理。JDK動態代理通過反射來接收被代理的類,並且要求被代理的類必須實現一個介面。JDK動態代理的核心是InvocationHandler介面和Proxy類。如果目標類沒有實現介面,那麼Spring AOP會選擇使用CGLIB來動態代理目標類。CGLIB(Code Generation Library),是一個代碼生成的類庫,可以在運行時動態的生成某個類的子類,注意,CGLIB是通過繼承的方式做的動態代理,因此如果某個類被標記為final,那麼它是無法使用CGLIB做動態代理的。為了驗證以上的說法,可以做一個簡單的測試。首先測試實現介面的情況。定義一個介面public interface Person { String sayHello(String name);}實現類@Componentpublic class Chinese implements Person { @Timer @Override public String sayHello(String name) { System.out.println("-- sayHello --"); return name + " hello, AOP"; } public void eat(String food) { System.out.println("我正在吃:" + food); }}這裡的@Timer註解是我自己定義的一個普通註解,用來標記Pointcut。定義Aspect@Aspect@Componentpublic class AdviceTest { @Pointcut("@annotation(com.listenzhangbin.aop.Timer)") public void pointcut { } @Before("pointcut") public void before { System.out.println("before"); }}運行@SpringBootApplication@RestControllerpublic class SpringBootDemoApplication { //這裡必須使用Person介面做注入 @Autowired private Person chinese; @RequestMapping("/test") public void test { chinese.sayHello("listen"); System.out.println(chinese.getClass); } public static void main(String args) { SpringApplication.run(SpringBootDemoApplication.class, args); }}輸出before-- sayHello --class com.sun.proxy.$Proxy53可以看到類型是com.sun.proxy.$Proxy53,也就是前面提到的Proxy類,因此這裡Spring AOP使用了JDK的動態代理。再來看看不實現介面的情況,修改Chinese類@Componentpublic class Chinese { @Timer// @Override public String sayHello(String name) { System.out.println("-- sayHello --"); return name + " hello, AOP"; } public void eat(String food) { System.out.println("我正在吃:" + food); } }運行@SpringBootApplication@RestControllerpublic class SpringBootDemoApplication { //直接用Chinese類注入 @Autowired private Chinese chinese; @RequestMapping("/test") public void test { chinese.sayHello("listen"); System.out.println(chinese.getClass); } public static void main(String args) { SpringApplication.run(SpringBootDemoApplication.class, args); }}輸出可以看到類被CGLIB增強了,也就是動態代理。這裡的CGLIB代理就是Spring AOP的代理,這個類也就是所謂的AOP代理,AOP代理類在切點動態地織入了增強處理。小結AspectJ在編譯時就增強了目標對象,Spring AOP的動態代理則是在每次運行時動態的增強,生成AOP代理對象,區別在於生成AOP代理對象的時機不同,相對來說AspectJ的靜態代理方式具有更好的性能,但是AspectJ需要特定的編譯器進行處理,而Spring AOP則無需特定的編譯器處理。加入群請加微信:2518988391(備註崗位)

本文由yidianzixun提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦