1. 程式人生 > >Spring的AOP實現原理

Spring的AOP實現原理

什麼是AOP

AOP(Aspect-OrientedProgramming,面向方面程式設計),可以說是OOP(Object-Oriented Programing,面向物件程式設計)的補充和完善。OOP引入封裝、繼承和多型性等概念來建立一種物件層次結構,用以模擬公共行為的一個集合。當我們需要為分散的物件引入公共行為的時候,OOP則顯得無能為力。也就是說,OOP允許你定義從上到下的關係,但並不適合定義從左到右的關係。例如日誌功能。日誌程式碼往往水平地散佈在所有物件層次中,而與它所散佈到的物件的核心功能毫無關係。對於其他型別的程式碼,如安全性、異常處理和透明的持續性也是如此。這種散佈在各處的無關的程式碼被稱為橫切(cross-cutting)程式碼,在OOP設計中,它導致了大量程式碼的重複,而不利於各個模組的重用。

而AOP技術則恰恰相反,它利用一種稱為“橫切”的技術,剖解開封裝的物件內部,並將那些影響了多個類的公共行為封裝到一個可重用模組,並將其名為“Aspect”,即方面。所謂“方面”,簡單地說,就是將那些與業務無關,卻為業務模組所共同呼叫的邏輯或責任封裝起來,便於減少系統的重複程式碼,降低模組間的耦合度,並有利於未來的可操作性和可維護性。AOP代表的是一個橫向的關係,如果說“物件”是一個空心的圓柱體,其中封裝的是物件的屬性和行為;那麼面向方面程式設計的方法,就彷彿一把利刃,將這些空心圓柱體剖開,以獲得其內部的訊息。而剖開的切面,也就是所謂的“方面”了。然後它又以巧奪天功的妙手將這些剖開的切面復原,不留痕跡。

使用“橫切”技術,AOP把軟體系統分為兩個部分:核心關注點和橫切關注點。業務處理的主要流程是核心關注點,與之關係不大的部分是橫切關注點。橫切關注點的一個特點是,他們經常發生在核心關注點的多處,而各處都基本相似。比如許可權認證、日誌、事務處理。Aop 的作用在於分離系統中的各種關注點,將核心關注點和橫切關注點分離開來。正如Avanade公司的高階方案構架師Adam Magee所說,AOP的核心思想就是“將應用程式中的商業邏輯同對其提供支援的通用服務進行分離。”

實現AOP的技術,主要分為兩大類:一是採用動態代理技術,利用擷取訊息的方式,對該訊息進行裝飾,以取代原有物件行為的執行;二是採用靜態織入的方式,引入特定的語法建立“方面”,從而使得編譯器可以在編譯期間織入有關“方面”的程式碼。

AOP使用場景

AOP用來封裝橫切關注點,具體可以在下面的場景中使用:

Authentication 許可權

Caching 快取

Context passing 內容傳遞

Error handling 錯誤處理

Lazy loading 懶載入

Debugging  除錯

logging, tracing, profiling and monitoring 記錄跟蹤 優化 校準

Performance optimization 效能優化

Persistence  持久化

Resource pooling 資源池

Synchronization 同步

Transactions 事務

AOP相關概念

方面(Aspect):一個關注點的模組化,這個關注點實現可能另外橫切多個物件。事務管理是J2EE應用中一個很好的橫切關注點例子。方面用spring的 Advisor或攔截器實現。

連線點(Joinpoint): 程式執行過程中明確的點,如方法的呼叫或特定的異常被丟擲。

通知(Advice): 在特定的連線點,AOP框架執行的動作。各種型別的通知包括“around”、“before”和“throws”通知。通知型別將在下面討論。許多AOP框架包括Spring都是以攔截器做通知模型,維護一個“圍繞”連線點的攔截器鏈。Spring中定義了四個advice: BeforeAdvice, AfterAdvice, ThrowAdvice和DynamicIntroductionAdvice

切入點(Pointcut): 指定一個通知將被引發的一系列連線點的集合。AOP框架必須允許開發者指定切入點:例如,使用正則表示式。 Spring定義了Pointcut介面,用來組合MethodMatcher和ClassFilter,可以通過名字很清楚的理解, MethodMatcher是用來檢查目標類的方法是否可以被應用此通知,而ClassFilter是用來檢查Pointcut是否應該應用到目標類上

引入(Introduction): 新增方法或欄位到被通知的類。 Spring允許引入新的介面到任何被通知的物件。例如,你可以使用一個引入使任何物件實現 IsModified介面,來簡化快取。Spring中要使用Introduction, 可有通過DelegatingIntroductionInterceptor來實現通知,通過DefaultIntroductionAdvisor來配置Advice和代理類要實現的介面

目標物件(Target Object): 包含連線點的物件。也被稱作被通知或被代理物件。POJO

AOP代理(AOP Proxy): AOP框架建立的物件,包含通知。 在Spring中,AOP代理可以是JDK動態代理或者CGLIB代理。

織入(Weaving): 組裝方面來建立一個被通知物件。這可以在編譯時完成(例如使用AspectJ編譯器),也可以在執行時完成。Spring和其他純Java AOP框架一樣,在執行時完成織入。

Spring AOP元件

下面這種類圖列出了Spring中主要的AOP元件


如何使用Spring AOP

可以通過配置檔案或者程式設計的方式來使用Spring AOP。

配置可以通過xml檔案來進行,大概有四種方式:

1.        配置ProxyFactoryBean,顯式地設定advisors, advice, target等

2.        配置AutoProxyCreator,這種方式下,還是如以前一樣使用定義的bean,但是從容器中獲得的其實已經是代理物件

3.        通過<aop:config>來配置

4.        通過<aop: aspectj-autoproxy>來配置,使用AspectJ的註解來標識通知及切入點

也可以直接使用ProxyFactory來以程式設計的方式使用Spring AOP,通過ProxyFactory提供的方法可以設定target物件, advisor等相關配置,最終通過 getProxy()方法來獲取代理物件

具體使用的示例可以google. 這裡略去

Spring AOP代理物件的生成

Spring提供了兩種方式來生成代理物件: JDKProxy和Cglib,具體使用哪種方式生成由AopProxyFactory根據AdvisedSupport物件的配置來決定。預設的策略是如果目標類是介面,則使用JDK動態代理技術,否則使用Cglib來生成代理。下面我們來研究一下Spring如何使用JDK來生成代理物件,具體的生成程式碼放在JdkDynamicAopProxy這個類中,直接上相關程式碼:

  1. /** 
  2.     * <ol> 
  3.     * <li>獲取代理類要實現的介面,除了Advised物件中配置的,還會加上SpringProxy, Advised(opaque=false) 
  4.     * <li>檢查上面得到的介面中有沒有定義 equals或者hashcode的介面 
  5.     * <li>呼叫Proxy.newProxyInstance建立代理物件 
  6.     * </ol> 
  7.     */
  8.    public Object getProxy(ClassLoader classLoader) {  
  9.        if (logger.isDebugEnabled()) {  
  10.            logger.debug("Creating JDK dynamic proxy: target source is " +this.advised.getTargetSource());  
  11.        }  
  12.        Class[] proxiedInterfaces =AopProxyUtils.completeProxiedInterfaces(this.advised);  
  13.        findDefinedEqualsAndHashCodeMethods(proxiedInterfaces);  
  14.        return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);  
  15. }  


那這個其實很明瞭,註釋上我也已經寫清楚了,不再贅述。

下面的問題是,代理物件生成了,那切面是如何織入的?

我們知道InvocationHandler是JDK動態代理的核心,生成的代理物件的方法呼叫都會委託到InvocationHandler.invoke()方法。而通過JdkDynamicAopProxy的簽名我們可以看到這個類其實也實現了InvocationHandler,下面我們就通過分析這個類中實現的invoke()方法來具體看下Spring AOP是如何織入切面的。

  1. publicObject invoke(Object proxy, Method method, Object[] args) throwsThrowable {  
  2.        MethodInvocation invocation = null;  
  3.        Object oldProxy = null;  
  4.        boolean setProxyContext = false;  
  5.        TargetSource targetSource = this.advised.targetSource;  
  6.        Class targetClass = null;  
  7.        Object target = null;  
  8.        try {  
  9.            //eqauls()方法,具目標物件未實現此方法
  10.            if (!this.equalsDefined && AopUtils.isEqualsMethod(method)){  
  11.                 return (equals(args[0])? Boolean.TRUE : Boolean.FALSE);  
  12.            }  
  13.            //hashCode()方法,具目標物件未實現此方法
  14.            if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)){  
  15.                 return newInteger(hashCode());  
  16.            }  
  17.            //Advised介面或者其父介面中定義的方法,直接反射呼叫,不應用通知
  18.            if (!this.advised.opaque &&method.getDeclaringClass().isInterface()  
  19.                     &&method.getDeclaringClass().isAssignableFrom(Advised.class)) {  
  20.                 // Service invocations onProxyConfig with the proxy config...
  21.                 return AopUtils.invokeJoinpointUsingReflection(this.advised,method, args);  
  22.            }  
  23.            Object retVal = null;  
  24.            if (this.advised.exposeProxy) {  
  25.                 // Make invocation available ifnecessary.
  26.                 oldProxy = AopContext.setCurrentProxy(proxy);  
  27.                 setProxyContext = true;  
  28.            }  
  29.            //獲得目標物件的類
  30.            target = targetSource.getTarget();  
  31.            if (target != null) {  
  32.                 targetClass = target.getClass();  
  33.            }  
  34.            //獲取可以應用到此方法上的Interceptor列表
  35.            List chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method,targetClass);  
  36.            //如果沒有可以應用到此方法的通知(Interceptor),此直接反射呼叫 method.invoke(target, args)
  37.            if (chain.isEmpty()) {  
  38.                 retVal = AopUtils.invokeJoinpointUsingReflection(target,method, args);  
  39.            } else {  
  40.                 //建立MethodInvocation
  41.                 invocation = newReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);  
  42.                 retVal = invocation.proceed();  
  43.            }  
  44.            // Massage return value if necessary.
  45.            if (retVal != null && retVal == target &&method.getReturnType().isInstance(proxy)  
  46.                     &&!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {  
  47.                 // Special case: it returned"this" and the return type of the method
  48.                 // is type-compatible. Notethat we can't help if the target sets
  49.                 // a reference to itself inanother returned object.
  50.                 retVal = proxy;  
  51.            }  
  52.            return retVal;  
  53.        } finally {  
  54.            if (target != null && !targetSource.isStatic()) {  
  55.                 // Must have come fromTargetSource.
  56.                targetSource.releaseTarget(target);  
  57.            }  
  58.            if (setProxyContext) {  
  59.                 // Restore old proxy.
  60.                 AopContext.setCurrentProxy(oldProxy);  
  61.            }  
  62.        }  
  63.     }  



主流程可以簡述為:獲取可以應用到此方法上的通知鏈(Interceptor Chain),如果有,則應用通知,並執行joinpoint; 如果沒有,則直接反射執行joinpoint。而這裡的關鍵是通知鏈是如何獲取的以及它又是如何執行的,下面逐一分析下。

首先,從上面的程式碼可以看到,通知鏈是通過Advised.getInterceptorsAndDynamicInterceptionAdvice()這個方法來獲取的,我們來看下這個方法的實現:

  1. public List<Object>getInterceptorsAndDynamicInterceptionAdvice(Method method, Class targetClass) {  
  2.                    MethodCacheKeycacheKey = new MethodCacheKey(method);  
  3.                    List<Object>cached = this.methodCache.get(cacheKey);  
  4.                    if(cached == null) {  
  5.                             cached= this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(  
  6.                                                this,method, targetClass);  
  7.                             this.methodCache.put(cacheKey,cached);  
  8.                    }  
  9.                    returncached;  
  10.          }  


可以看到實際的獲取工作其實是由AdvisorChainFactory. getInterceptorsAndDynamicInterceptionAdvice()這個方法來完成的,獲取到的結果會被快取。

下面來分析下這個方法的實現:

  1. /** 
  2.     * 從提供的配置例項config中獲取advisor列表,遍歷處理這些advisor.如果是IntroductionAdvisor, 
  3.     * 則判斷此Advisor能否應用到目標類targetClass上.如果是PointcutAdvisor,則判斷 
  4.     * 此Advisor能否應用到目標方法method上.將滿足條件的Advisor通過AdvisorAdaptor轉化成Interceptor列表返回. 
  5.     */
  6.     publicList getInterceptorsAndDynamicInterceptionAdvice(Advised config, Methodmethod, Class targetClass) {  
  7.        // This is somewhat tricky... we have to process introductions first,
  8.        // but we need to preserve order in the ultimate list.
  9.        List interceptorList = new ArrayList(config.getAdvisors().length);  
  10.        //檢視是否包含IntroductionAdvisor
  11.        boolean hasIntroductions = hasMatchingIntroductions(config,targetClass);  
  12.        //這裡實際上註冊一系列AdvisorAdapter,用於將Advisor轉化成MethodInterceptor
  13.        AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();  
  14.        Advisor[] advisors = config.getAdvisors();  
  15.         for (int i = 0; i <advisors.length; i++) {  
  16.            Advisor advisor = advisors[i];  
  17.            if (advisor instanceof PointcutAdvisor) {  
  18.                 // Add it conditionally.
  19.                 PointcutAdvisor pointcutAdvisor= (PointcutAdvisor) advisor;  
  20.                 if(config.isPreFiltered() ||pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) {  
  21.                     //TODO: 這個地方這兩個方法的位置可以互換下
  22.                     //將Advisor轉化成Interceptor
  23.                     MethodInterceptor[]interceptors = registry.getInterceptors(advisor);  
  24.                     //檢查當前advisor的pointcut是否可以匹配當前方法
  25.                     MethodMatcher mm =pointcutAdvisor.getPointcut().getMethodMatcher();  
  26.                     if (MethodMatchers.matches(mm,method, targetClass, hasIntroductions)) {  
  27.                         if(mm.isRuntime()) {  
  28.                             // Creating a newobject instance in the getInterceptors() method
  29.                             // isn't a problemas we normally cache created chains.
  30.                             for (intj = 0; j < interceptors.length; j++) {  
  31.                                interceptorList.add(new InterceptorAndDynamicMethodMatcher(interceptors[j],mm));  
  32.                             }  
  33.                         } else {  
  34.                             interceptorList.addAll(Arrays.asList(interceptors));  
  35.                         }  
  36.                     }  
  37.                 }  
  38.            } elseif (advisor instanceof IntroductionAdvisor){  
  39.                 IntroductionAdvisor ia =(IntroductionAdvisor) advisor;  
  40.                 if(config.isPreFiltered() || ia.getClassFilter().matches(targetClass)) {  
  41.                     Interceptor[] interceptors= registry.getInterceptors(advisor);  
  42.                     interceptorList.addAll(Arrays.asList(interceptors));  
  43.                 }  
  44.            } else {  
  45.                 Interceptor[] interceptors =registry.getInterceptors(advisor);  
  46.                 interceptorList.addAll(Arrays.asList(interceptors));  
  47.            }  
  48.        }  
  49.        return interceptorList;  
  50. }  


這個方法執行完成後,Advised中配置能夠應用到連線點或者目標類的Advisor全部被轉化成了MethodInterceptor.

接下來我們再看下得到的攔截器鏈是怎麼起作用的。

  1. if (chain.isEmpty()) {  
  2.                 retVal = AopUtils.invokeJoinpointUsingReflection(target,method, args);  
  3.             } else {  
  4.                 //建立MethodInvocation
  5.                 invocation = newReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);  
  6.                 retVal = invocation.proceed();  
  7.             }  


         從這段程式碼可以看出,如果得到的攔截器鏈為空,則直接反射呼叫目標方法,否則建立MethodInvocation,呼叫其proceed方法,觸發攔截器鏈的執行,來看下具體程式碼

  1. public Object proceed() throws Throwable {  
  2.        //  We start with an index of -1and increment early.
  3.        if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size()- 1) {  
  4.            //如果Interceptor執行完了,則執行joinPoint
  5.            return invokeJoinpoint();  
  6.        }  
  7.        Object interceptorOrInterceptionAdvice =  
  8.            this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);  
  9.        //如果要動態匹配joinPoint
  10.        if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher){  
  11.            // Evaluate dynamic method matcher here: static part will already have
  12.            // been evaluated and found to match.
  13.            InterceptorAndDynamicMethodMatcher dm =  
  14.                 (InterceptorAndDynamicMethodMatcher)interceptorOrInterceptionAdvice;  
  15.            //動態匹配:執行時引數是否滿足匹配條件
  16.            if (dm.methodMatcher.matches(this.method, this.targetClass,this.arguments)) {  
  17.                 //執行當前Intercetpor
  18.                 returndm.interceptor.invoke(this);  
  19.            }  
  20.            else {  
  21.                 //動態匹配失敗時,略過當前Intercetpor,呼叫下一個Interceptor
  22.                 return proceed();  
  23.            }  
  24.        }  
  25.        else {  
  26.            // It's an interceptor, so we just invoke it: The pointcutwill have
  27.            // been evaluated statically before this object was constructed.
  28.            //執行當前Intercetpor
  29.            return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);  
  30.        }  
  31. }  


程式碼也比較簡單,這裡不再贅述。