Spring AOP源碼剖析——跟進(jìn)中......

之前就Spring AOP的基本術(shù)語和原理及使用有了一定的認(rèn)識(shí),作為Spring核心特性之一,AOP同樣很有必要重點(diǎn)掌握。這次沉淀將會(huì)開啟AOP源碼閱讀的序幕。

對(duì)于源碼的理解,以注釋添加在對(duì)應(yīng)代碼塊上方

一、AOP概念回顧

為什么會(huì)有面向切面編程(AOP)?眾所周知,Java是一個(gè)面向?qū)ο?OOP)的語言,但它有一些弊端,例如:當(dāng)我們需要為多個(gè)不具有繼承關(guān)系的對(duì)象引入一個(gè)公共行為,例如日志、權(quán)限、事務(wù)、性能監(jiān)測等功能時(shí),只能在在每個(gè)對(duì)象里引用公共行為,這樣做不便于維護(hù),而且有大量重復(fù)代碼,通常我們更希望的是這些模塊可以實(shí)現(xiàn)熱插拔特性而且無需把外圍的代碼入侵到核心模塊中。

為了能夠更好地將系統(tǒng)級(jí)別的代碼抽離出來,去掉與對(duì)象的耦合,就產(chǎn)生了面向AOP(面向切面)。



如上圖所示,OOP屬于一種橫向擴(kuò)展,AOP是一種縱向擴(kuò)展。AOP依托于OOP,進(jìn)一步將系統(tǒng)級(jí)別的代碼抽象出來,進(jìn)行縱向排列,實(shí)現(xiàn)低耦合。

AOP的出現(xiàn)彌補(bǔ)了OOP的這點(diǎn)不足。假設(shè)現(xiàn)在我們把日志、權(quán)限、事務(wù)、性能監(jiān)測等外圍業(yè)務(wù)看作單獨(dú)的關(guān)注點(diǎn)(也可以理解為單獨(dú)的模塊),每個(gè)關(guān)注點(diǎn)都可以在需要它們的時(shí)刻及時(shí)被運(yùn)用而且無需提前整合到核心模塊中。

二、源碼剖析

1. Demo

  public static void main(String[] args) {
        //創(chuàng)建代理工廠
        ProxyFactory proxyFactory = new ProxyFactory();
        //設(shè)置目標(biāo)對(duì)象
        proxyFactory.setTarget(new MyLogServiceImpl());
        //前置增強(qiáng)
        proxyFactory.addAdvice(new MyLogBefore());
        //后置增強(qiáng)
        proxyFactory.addAdvice(new MyLogAfter());
        //從代理工廠中獲取代理
        MyLogService myLogService = (MyLogService) proxyFactory.getProxy();
        myLogService.log("x");
   }

2. 源碼剖析

在demo中,直接獲取的是一個(gè)代理,不是要使用的實(shí)現(xiàn)類,這是因?yàn)锳OP其實(shí)就是代理模式,在編譯期或者運(yùn)行期,給我們?cè)瓉淼拇a增加一些功能,成為一個(gè)代理。當(dāng)我們調(diào)用的時(shí)候,實(shí)際就是調(diào)用的代理類。

代碼中首先創(chuàng)建一個(gè)代理工廠實(shí)例:

ProxyFactory proxyFactory = new ProxyFactory();

代理工廠的作用就是使用編程的方式創(chuàng)建AOP代理。ProxyFactory繼承自AdvisedSupport,AdvicedSupport是AOP代理的配置管理器。

需要明白的是,Spring中實(shí)現(xiàn)AOP,就是生成一個(gè)代理,然后在使用的時(shí)候調(diào)用代理。

首先從方法proxyFactory.setTarget(new LoginServiceImpl())開始剖析源碼:

public void setTarget(Object target) {
    /**首先根據(jù)給定的目標(biāo)實(shí)現(xiàn)類,創(chuàng)建一個(gè)單例的TargetSource
    **然后設(shè)置TargetSource
    */
    setTargetSource(new SingletonTargetSource(target));
}

類SingletonTargetSource實(shí)現(xiàn)接口TargetSource :

public interface TargetSource {
    //返回目標(biāo)類的類型
    Class getTargetClass();
    
    /**查看TargetSource是否是static的
    *靜態(tài)的TargetSource每次都返回同一個(gè)Target
    */
    boolean isStatic();
    
    //獲取目標(biāo)類的實(shí)例
    Object getTarget() throws Exception;
    
    //釋放目標(biāo)類
    void releaseTarget(Object target) throws Exception;
}

類SingletonTargetSource定義如下:

public final class SingletonTargetSource implements TargetSource, Serializable {

    //用來保存目標(biāo)類   
    private final Object target;
    //構(gòu)造方法
    public SingletonTargetSource(Object target) {
        this.target = target;
    }
    //直接返回目標(biāo)類的類型
    public Class getTargetClass() {
        return target.getClass();
    }
    //返回目標(biāo)類
    public Object getTarget() {
        return this.target;
    }
    //釋放目標(biāo)類,代碼邏輯為空
    public void releaseTarget(Object o) {
        // Nothing to do
    }
    //是否為靜態(tài),這里直接返回true
    public boolean isStatic() {
        return true;
    }

    //重寫equals方法
    public boolean equals(Object other) {
        //相等,返回true
        if (this == other) {
            return true;
        }
        //如果不是SingletonTargetSource類型的返回false
        if (!(other instanceof SingletonTargetSource)) {
            return false;
        }
        SingletonTargetSource otherTargetSource = (SingletonTargetSource) other;
        //判斷目標(biāo)類是否相等
        return ObjectUtils.nullSafeEquals(this.target, otherTargetSource.target);
    }
    
    //重寫toString方法
    public String toString() {
        return "SingletonTargetSource: target=(" + target + ")";
    }
}

在AdvisedSupport類中的設(shè)置目標(biāo)類setTargetSource方法:

public void setTargetSource(TargetSource targetSource) {
    if (isActive() && getOptimize()) {
        throw new AopConfigException("Can't change target with an optimized CGLIB proxy: it has its own target");
    }
    //將構(gòu)建的TargetSource緩存起來
    this.targetSource = targetSource;
}

設(shè)置了要代理的目標(biāo)類之后,接下來就是添加通知,即添加增強(qiáng)類,proxyFactory.addAdvice()方法是添加增強(qiáng)類的方法。對(duì)應(yīng)demo中是:

        //前置增強(qiáng)
        proxyFactory.addAdvice(new MyLogBefore());
        //后置增強(qiáng)
        proxyFactory.addAdvice(new MyLogAfter());

addAdvice方法的參數(shù)是一個(gè)Advice類型的類,也就是通知或者叫增強(qiáng),這里先介紹有關(guān)通知Advice的代碼。

Advice接口

Advice不屬于Spring,是AOP聯(lián)盟定義的接口。Advice接口并沒有定義任何方法,是一個(gè)空的接口,用來做標(biāo)記,實(shí)現(xiàn)了此接口的的類是一個(gè)通知類。Advice有幾個(gè)子接口:

  • BeforeAdvice,前置增強(qiáng),即在我們的目標(biāo)類之前調(diào)用的增強(qiáng),未定義任何方法;
  • AfterReturningAdvice,方法正常返回前的增強(qiáng),該增強(qiáng)可以看到方法的返回值,但不能更改返回值;
  • ThrowsAdvice,拋出異常時(shí)候的增強(qiáng),是一個(gè)標(biāo)志接口,未定義任何方法;
  • Interceptor,攔截器,未定義任何方法,表示一個(gè)通用的攔截器。不屬于Spring;
  • DynamicIntroductionAdvice,動(dòng)態(tài)引介增強(qiáng),有一個(gè)方法implementsInterface。

MethodBeforeAdvice接口,是BeforeAdvice的子接口,表示在方法前調(diào)用的增強(qiáng)。

public interface MethodBeforeAdvice extends BeforeAdvice {
    
    /**在給定的方法調(diào)用前,調(diào)用該方法
    *參數(shù)method是被代理的方法
    *參數(shù)args是被代理方法的參數(shù)
    *參數(shù)target是方法調(diào)用的目標(biāo),可能為null
    */
    void before(Method m, Object[] args, Object target) throws Throwable;
}

我們來看看向代理工廠中添加增強(qiáng)的addAdvice方法,addAdvice方法在AdvisedSupport類中:

public void addAdvice(Advice advice) throws AopConfigException {
    /**advisors是Advice列表,是一個(gè)LinkedList
    *如果被添加進(jìn)來的是一個(gè)Interceptor,會(huì)先被包裝成一個(gè)Advice
    *添加之前現(xiàn)獲取advisor的大小,當(dāng)做添加的Advice的位置
    */
    int pos = (this.advisors != null) ? this.advisors.size() : 0;
    //添加Advice
    addAdvice(pos, advice);
}

未完跟進(jìn)中......(addAdvice(pos, advice)方法)

方法addAdvice(pos, advice)源碼:

//添加Advice
public void addAdvice(int pos, Advice advice) throws AopConfigException {
    //只能處理已經(jīng)實(shí)現(xiàn)了AOP聯(lián)盟的接口的攔截器
    if (advice instanceof Interceptor && !(advice instanceof MethodInterceptor)) {
        throw new AopConfigException(getClass().getName() + " only handles AOP Alliance MethodInterceptors");
    }
    /**如果advice是IntroductionInfo接口類型,
    *不需要IntroductionAdvisor
    */
    if (advice instanceof IntroductionInfo) {
        addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice));
    }
    /**如果advice是動(dòng)態(tài)引介增強(qiáng),
    *需要IntroductionAdvisor
    */
    else if (advice instanceof DynamicIntroductionAdvice) {
        throw new AopConfigException("DynamicIntroductionAdvice may only be added as part of IntroductionAdvisor");
    }
    else {
        //添加增強(qiáng)器,需要先將增強(qiáng)包裝成增強(qiáng)器,然后進(jìn)行添加
        addAdvisor(pos, new DefaultPointcutAdvisor(advice));
    }
}

添加增強(qiáng)的過程:實(shí)際調(diào)用添加增強(qiáng)器這個(gè)方法

  1. 將Advice包裝成一個(gè)PointCutAdvisor;
  2. 然后在添加增強(qiáng)器。

Advisor接口

Advisor,增強(qiáng)器,它持有一個(gè)增強(qiáng)Advice和一個(gè)過濾器,用來決定Advice的所使用的位置。

public interface Advisor {
    
    //判斷Advice是否存在于每個(gè)實(shí)例中
    boolean isPerInstance();
    
    //返回當(dāng)下持有的Advice
    Advice getAdvice();

}

PointcutAdvisor

PointcutAdvisor是一個(gè)持有Pointcut切點(diǎn)的增強(qiáng)器,擁有一個(gè)Advice和一個(gè)Pointcut。

public interface PointcutAdvisor extends Advisor {
    //獲取Pointcut
    Pointcut 
}

Pointcut接口

即切入點(diǎn),定義了哪些連接點(diǎn)需要被織入橫切邏輯。

public interface Pointcut {
    //類過濾器,用于明確需要攔截的類
    ClassFilter getClassFilter();
    //方法匹配器,用于明確需要攔截的方法
    MethodMatcher getMethodMatcher();
    
    Pointcut TRUE = TruePointcut.INSTANCE; 
}

ClassFilter接口

public interface ClassFilter {
    //判斷所給的類是否需要攔截
    boolean matches(Class clazz);

    ClassFilter TRUE = TrueClassFilter.INSTANCE;
}

MethodMatcher接口

public interface MethodMatcher {    
    //靜態(tài)方法匹配
    boolean matches(Method m, Class targetClass);
    //是否是運(yùn)行時(shí)動(dòng)態(tài)匹配
    boolean isRuntime();
    //運(yùn)行時(shí)動(dòng)態(tài)匹配
    boolean matches(Method m, Class targetClass, Object[] args);
    
    MethodMatcher TRUE = TrueMethodMatcher.INSTANCE;

}

了解了這些相關(guān)定義之后,回到源碼邏輯的剖析,讓我們來看看添加增強(qiáng)器方法addAdvisor的具體實(shí)現(xiàn):

 public void addAdvisor(int pos, Advisor advisor) throws AopConfigException {
    //如果增強(qiáng)器是引介增強(qiáng)器
    if (advisor instanceof IntroductionAdvisor) {
        addAdvisor(pos, (IntroductionAdvisor) advisor);
    }
    else {
        //其他的增強(qiáng)器處理
        addAdvisorInternal(pos, advisor);
    }
}

方法 addAdvisorInternal(pos, advisor)的源碼:

private void addAdvisorInternal(int pos, Advisor advice) throws AopConfigException {
    if (isFrozen()) {
        throw new AopConfigException("Cannot add advisor: config is frozen");
    }
    //把Advice添加到LinkedList中指定位置pos
    this.advisors.add(pos, advice);
    //同時(shí)更新Advisors數(shù)組
    updateAdvisorArray();
    //通知監(jiān)聽器
    adviceChanged();
}

獲取代理

上述源碼是在組裝代理工廠,接下類我們繼而剖析代理的生成,方法proxyFactory.getProxy()這一步就是獲取代理的過程:

public Object getProxy() {
    //創(chuàng)建AOP代理
    AopProxy proxy = createAopProxy();
    //返回代理
    return proxy.getProxy();
}

Spring中,創(chuàng)建代理通常有兩種方式:

  1. JDK動(dòng)態(tài)代理;
  2. CGLIB動(dòng)態(tài)代理。
    而這里的創(chuàng)建AOP代理就是生成這兩種代理中的一種。
protected synchronized AopProxy createAopProxy() {
    if (!this.isActive) {
        activate();
    }
    //獲取AOP代理工廠,然后創(chuàng)建代理
    return getAopProxyFactory().createAopProxy(this);
}
public AopProxy createAopProxy(AdvisedSupport advisedSupport) throws AopConfigException {
    //判斷使用ObjenesisCglibAopProxy還是JdkDynamicAopProxy
    /**如果代理的是類,
    *就使用CGLIB的方式來創(chuàng)建代理
    */    
boolean useCglib = advisedSupport.getOptimize() || advisedSupport.getProxyTargetClass() || advisedSupport.getProxiedInterfaces().length == 0;
    if (useCglib) {
        return CglibProxyFactory.createCglibProxy(advisedSupport);
    }
    else {
        /**如果代理的是接口,
        *就使用JDK動(dòng)態(tài)代理來創(chuàng)建代理
        */    
        return new JdkDynamicAopProxy(advisedSupport);
    }
}

JDK動(dòng)態(tài)代理

看下JDK動(dòng)態(tài)代理的方式,對(duì)于方法的調(diào)用,調(diào)用的是代理類的invoke方法:

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    MethodInvocation invocation = null;
    Object oldProxy = null;
    boolean setProxyContext = false;
    //所代理的目標(biāo)對(duì)象
    TargetSource targetSource = advisedSupport.targetSource;
    Class targetClass = null;
    Object target = null;       

    try {
        //equal、hashCode 等方法
        if (method.getDeclaringClass() == Object.class && "equals".equals(method.getName())) {
            return equals(args[0]) ? Boolean.TRUE : Boolean.FALSE;
        }
        else if (Advised.class == method.getDeclaringClass()) {
            return AopProxyUtils.invokeJoinpointUsingReflection(this.advisedSupport, method, args);
        }

        Object retVal = null;

        //代理目標(biāo)對(duì)象
        target = targetSource.getTarget();
        if (target != null) {
            targetClass = target.getClass();
        }
        if (this.advisedSupport.exposeProxy) {
            // Make invocation available if necessary
            oldProxy = AopContext.setCurrentProxy(proxy);
            setProxyContext = true;
        }

        //獲取此方法的攔截鏈
        List chain = this.advisedSupport.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
                this.advisedSupport, proxy, method, targetClass);

        //如果沒有配置通知
        if (chain.isEmpty()) {
            //直接調(diào)用目標(biāo)對(duì)象的方法
            retVal = AopProxyUtils.invokeJoinpointUsingReflection(target, method, args);
        }
        else {
            //如果配置了通知,創(chuàng)建一個(gè)方法調(diào)用
            invocation = new ReflectiveMethodInvocation(proxy, target,
                                method, args, targetClass, chain);

            //執(zhí)行通知鏈,沿著通知器鏈調(diào)用所有的通知
            retVal = invocation.proceed();
        }
        if (retVal != null && retVal == target) {
            //返回值為自己
            retVal = proxy;
        }
        //返回
        return retVal;
    }
    finally {
        if (target != null && !targetSource.isStatic()) {
            targetSource.releaseTarget(target);
        }

        if (setProxyContext) {
            AopContext.setCurrentProxy(oldProxy);
        }
    }
}

上述方法步驟:

  1. 如果攔截鏈不為空,則創(chuàng)建一個(gè)ReflectiveMethodInvocation;
  2. 調(diào)用其proceed方法;3
  3. proceed方法的調(diào)用會(huì)遞歸調(diào)用,直到所有的MethodInterceptor調(diào)用完
  4. 如果攔截鏈為空,直接調(diào)用目標(biāo)對(duì)象的方法。

未完跟進(jìn)中......

參考

https://juejin.im/post/591d8c8ba22b9d00585007dd

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文是我自己在秋招復(fù)習(xí)時(shí)的讀書筆記,整理的知識(shí)點(diǎn),也是為了防止忘記,尊重勞動(dòng)成果,轉(zhuǎn)載注明出處哦!如果你也喜歡,那...
    波波波先森閱讀 12,455評(píng)論 6 86
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,319評(píng)論 25 708
  • 等待是一種沒有限度的愛,等待著你的問候,等待著你的信息,等待著你的電話,等待也是一種煎熬…… 等待承載了太多的思念...
    榮耀啟程閱讀 321評(píng)論 0 2
  • 每天早出晚歸,跟孩子相處的時(shí)間僅有幾個(gè)小時(shí),這兩天不斷使用林老師的方法,讓我懂得和孩子的相處要學(xué)會(huì)接受同時(shí)也要不斷...
    英才日記閱讀 221評(píng)論 0 0

友情鏈接更多精彩內(nèi)容