1. 程式人生 > >Spring AOP高級——源碼實現(2)Spring AOP中通知器(Advisor)與切面(Aspect)

Spring AOP高級——源碼實現(2)Spring AOP中通知器(Advisor)與切面(Aspect)

color oaf 小麻煩 ntc tro sta ins pack package

本文例子完整源碼地址:https://github.com/yu-linfeng/BlogRepositories/tree/master/repositories/Spring%20AOP%E9%AB%98%E7%BA%A7%E2%80%94%E2%80%94%E6%BA%90%E7%A0%81%E5%AE%9E%E7%8E%B0%EF%BC%882%EF%BC%89Spring%20AOP%E4%B8%AD%E9%80%9A%E7%9F%A5%E5%99%A8%EF%BC%88Advisor%EF%BC%89%E4%B8%8E%E5%88%87%E9%9D%A2%EF%BC%88Aspect%EF%BC%89

  之所以還未正式進入Spring AOP的源碼,是因為我在閱讀Spring AOP生成代理對象時遇到了一點小麻煩讓我不得不暫時停止,轉而理清有關Spring AOP中的兩個概念性問題。

  前面的博客裏都沒有提到過“通知器”這個概念,在《Spring實戰》書中也只是簡單地說明了在xml中<aop:advisor>用於定義一個通知器,此後便沒再說明,而是使用<aop:aspect>定義一個切面。而在《Spring技術內幕》中有關Spring AOP章節中則是介紹了AOP中三個概念:通知、切點、通知器。在這時,我對“通知器”產生了很大的疑惑,查閱了相關資料並沒有滿意的答案,於是決定自己一探究竟。

  首先來討論定義通知器相關的使用方法。 定義一個通知類,其中包含前置通知和後置通知,註意如果是使用<aop:advisor>定義通知器的方式實現AOP則需要通知類實現Advice接口,前置通知方法對應的是MethodBeforeAdvice,後置通知方法對應的是AfterReturningAdvice。

 1 package com.demo;
 2 
 3 import org.springframework.aop.AfterReturningAdvice;
 4 import org.springframework.aop.MethodBeforeAdvice;
 5 import org.springframework.stereotype.Component;
 6 
 7 import java.lang.reflect.Method;
 8 
 9 /**
10  * Created by Kevin on 2017/11/15.
11  */
12 @Component("advisorTest")
13 public class AdvisorTest implements MethodBeforeAdvice, AfterReturningAdvice{ 14 15 /** 16 * 前置通知 17 * @param method 18 * @param args 19 * @param target 20 * @throws Throwable 21 */ 22 @Override 23 public void before(Method method, Object[] args, Object target) throws Throwable { 24 System.out.println("前置通知"); 25 } 26 27 /** 28 * 後置通知 29 * @param returnValue 30 * @param method 31 * @param args 32 * @param target 33 * @throws Throwable 34 */ 35 @Override 36 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { 37 System.out.println("後置通知"); 38 } 39 }

  定義一個需要被代理的目標對象。

 1 package com.demo;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6  * 目標對象,需要被代理的類及方法
 7  * Created by Kevin on 2017/11/15.
 8  */
 9 @Component("testPoint")
10 public class TestPoint {
11 
12     public void test() {
13         System.out.println("方法調用");
14     }
15 }

  我們要達到的目的就是在test方法調用前和調用後分別打印“前置通知”和“後置通知”。

  applicationContext.xml中定義通知器如下:

 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:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 7 
 8     <context:component-scan base-package="com.demo"/>
 9 
10     <aop:config>
11         <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>
12         <aop:advisor advice-ref="advisorTest" pointcut-ref="test"/>
13     </aop:config>
14 
15 </beans>

  最後的運行結果符合預期。那麽問題來了,如果我們只想在定義的這個切點 <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>裏只配置前置通知,這個時候怎麽辦呢?答案是,通過以上方式是不可以的。也就是說如果通過定義Advisor的方式,在有的地方比較局限,狹隘來講通過定義Advisor通知器的方式,只能定義只有一個通知和一個切入點的切面。當然一個通知不準確,因為上面可以看到只要實現不同的通知接口即可代理,但如果實現了多個通知接口,而只想使用一個時就不可以了。通知器是一個特殊的切面。

  接著來討論定義切面相關的使用方法。 如果使用<aop:aspect>定義切面的方式,通知類是可以不用實現任何通知接口的,這是很大一個便利。同樣要實現上面例子的功能,定義一個通知類,包括前置通知和後置通知。

 1 package com.demo;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 /**
 6  * Created by Kevin on 2017/11/15.
 7  */
 8 @Component("aspectTest")
 9 public class AspectTest {
10 
11     /**
12      * 前置通知
13      */
14     public void doBefore() {
15         System.out.println("前置通知");
16     }
17 
18     /**
19      * 後置通知
20      */
21     public void doAfter() {
22         System.out.println("後置通知");
23     }
24 }

  目標對象和上面的例子一致,緊接著是applicationContext.xml中切面的配置。

 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:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 7 
 8     <context:component-scan base-package="com.demo"/>
 9 
10     <aop:config>
11         <aop:aspect ref="aspectTest">
12             <aop:pointcut id="test" expression="execution(* com.demo.TestPoint.test())"/>
13             <aop:before method="doBefore" pointcut-ref="test"/>
14             <aop:after-returning method="doAfter" pointcut-ref="test"/>
15         </aop:aspect>
16     </aop:config>
17 </beans>

  可以看到我們通過<aop:aspect>定義了一個切面,如果只需要前置通知,則只定義<aop:before>就可以了,這和<aop:advisor>是很大的不同,由此可知通過<aop:aspect>定義切面的方式可以在其中靈活地定義通知,而不必像通知器那樣約束。

  實際上可以這麽說,通知器是一個特殊的切面。而在最開始那兩篇博客中沒有提到是因為那兩個例子中使用的是AspectJ註解,而在AspectJ註解中並沒有與此對應的概念。

  在實際中用到的<aop:advisor>場景最多的莫過於在Spring中配置事務。除此之外,很少用到,也不建議使用。因為最大的一個問題就是定義通知時需要實現通知接口,這違背了一點Spring“非侵入式”編程的初衷。

  這篇博客穿插在源碼的其中是為了更好的理清Spring AOP中各種概念問題,緣由我在開頭已經說過,接下來就正式開始Spring AOP源碼的解讀。

這是一個能給程序員加buff的公眾號

技術分享

Spring AOP高級——源碼實現(2)Spring AOP中通知器(Advisor)與切面(Aspect)