Spring AOP 源碼解析三(代理對象的創(chuàng)建)

引言

上期我們對AOP核心概念及接口做了粗淺的分析,這期我們主要來探討一下代理對象的創(chuàng)建過程凭疮。在開始之前次坡,先問自己幾個問題

Spring是如何幫我們?nèi)ミx擇合適的Advice的幽歼?找到了又是通過何種方式創(chuàng)建代理對象的?

好了讯嫂,現(xiàn)在我們開始分析代理對象的創(chuàng)建過程,首先兆沙,先來看一下AspectJAwareAdvisorAutoProxyCreator.class類的繼承體系

image.png

可以看到AspectJAwareAdvisorAutoProxyCreator.class主要實現(xiàn)了這幾個接口

  1. Aware Bean創(chuàng)建時注入一些容器屬性等
  2. BeanPostProcessor IOC 擴(kuò)展點
  3. AopInfrastructureBean 標(biāo)識此類是AOP系統(tǒng)類欧芽,不能被代理

這里我們僅需要關(guān)注BeanPostProcessor接口的實現(xiàn),因為代理的創(chuàng)建是在BeanPostProcessor接口的postProcessAfterInitialization方法執(zhí)行時創(chuàng)建的葛圃,postProcessAfterInitialization方法的具體實現(xiàn)邏輯是在抽象父類AbstractAutoProxyCreator中實現(xiàn)的

入口

上文中提到AspectJAwareAdvisorAutoProxyCreator.classBeanPostProcessor接口的實現(xiàn)千扔,現(xiàn)在就讓我們打開它的入口源碼慢慢分析

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean != null) {
        // 返回用做緩存的key鍵
        Object cacheKey = getCacheKey(bean.getClass(), beanName);
        if (!this.earlyProxyReferences.contains(cacheKey)) {
            return wrapIfNecessary(bean, beanName, cacheKey);
        }
    }
    return bean;
}
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
    if (beanName != null && this.targetSourcedBeans.contains(beanName)) {
        return bean;
    }
    if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
        return bean;
    }
    // 判斷當(dāng)前的Bean是否是AOP本身的系統(tǒng)類,是的話則跳過库正,不做代理操作
    if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
        this.advisedBeans.put(cacheKey, Boolean.FALSE);
        return bean;
    }

    // 為該Bean找到一個合適的 advisor
    Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
    // 返回不為空
    if (specificInterceptors != DO_NOT_PROXY) {
        this.advisedBeans.put(cacheKey, Boolean.TRUE);
        // 創(chuàng)建代理對象
        Object proxy = createProxy(
                bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
        this.proxyTypes.put(cacheKey, proxy.getClass());
        return proxy;
    }
    
    // 沒有找到合適的 advisor 則直接返回原始Bean
    this.advisedBeans.put(cacheKey, Boolean.FALSE);
    return bean;
}

看完wrapIfNecessary方法曲楚,這里就是創(chuàng)建代理對象的全過程。其實在這個方法中一共就做了兩件事情:

  1. getAdvicesAndAdvisorsForBean方法 篩選合適的Advisor對象褥符,其實就是為當(dāng)前目標(biāo)對象找到合適的通知(Advice)
  2. createProxy方法 根據(jù)篩選到的切面(Advisor)集合為目標(biāo)對象創(chuàng)建代理

下面我們分別對這兩件事情作分析

尋找合適的 Advisor

findEligibleAdvisors方法是在父類AbstractAdvisorAutoProxyCreator中龙誊,
且直接調(diào)用了findEligibleAdvisors 方法

    List<Advisor> advisors = findEligibleAdvisors(beanClass, beanName);
    if (advisors.isEmpty()) {
        return DO_NOT_PROXY;
    }
    return advisors.toArray();
}
protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
    // 找到容器中的全部通知
    List<Advisor> candidateAdvisors = findCandidateAdvisors();
    // 篩選合適通知對象集合
    List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
    extendAdvisors(eligibleAdvisors);
    if (!eligibleAdvisors.isEmpty()) {
        eligibleAdvisors = sortAdvisors(eligibleAdvisors);
    }
    return eligibleAdvisors;
}

這里也是分開做了兩件事情

  1. 首先從Spring 容器中獲取到全部Advisors集合
  2. 執(zhí)行篩選邏輯,獲取符合條件的Advisors集合

找到Spring容器中全部 Advisor

這里是調(diào)用了BeanFactoryAdvisorRetrievalHelper.class的方法去獲取到Spring 容器中全部Advisor集合的属瓣,這里需要注意BeanFactoryAdvisorRetrievalHelper.class類的初始化是在setBeanFactory方法中進(jìn)行的

protected List<Advisor> findCandidateAdvisors() {
    return this.advisorRetrievalHelper.findAdvisorBeans();
}
  • BeanFactoryAdvisorRetrievalHelperfindAdvisorBeans方法
public List<Advisor> findAdvisorBeans() {
    // Determine list of advisor bean names, if not cached already.
    String[] advisorNames = null;
    synchronized (this) {
        // 嘗試從緩存中獲取 Advisor 類型的Bean名稱
        advisorNames = this.cachedAdvisorBeanNames;
        // 
        if (advisorNames == null) {
            // 從BeanFactory中獲取 Advisor 類型的全部Bean名稱集合载迄,并設(shè)置緩存
            advisorNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
                    this.beanFactory, Advisor.class, true, false);
            this.cachedAdvisorBeanNames = advisorNames;
        }
    }
    if (advisorNames.length == 0) {
        return new LinkedList<Advisor>();
    }

    List<Advisor> advisors = new LinkedList<Advisor>();
    // 遍歷 Advisor類型 BeanName 集合
    for (String name : advisorNames) {
        // 判斷是否合法讯柔,這里返回都是true
        if (isEligibleBean(name)) {
            // 判斷該Bean是否正在創(chuàng)建中抡蛙,創(chuàng)建中則直接跳過
            if (this.beanFactory.isCurrentlyInCreation(name)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Skipping currently created advisor '" + name + "'");
                }
            }
            else {
                try {
                    // 根據(jù)BeanName和Advisor類型從BeanFactory中獲取Bean
                    advisors.add(this.beanFactory.getBean(name, Advisor.class));
                }
                catch (BeanCreationException ex) {
                    Throwable rootCause = ex.getMostSpecificCause();
                    if (rootCause instanceof BeanCurrentlyInCreationException) {
                        BeanCreationException bce = (BeanCreationException) rootCause;
                        if (this.beanFactory.isCurrentlyInCreation(bce.getBeanName())) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Skipping advisor '" + name +
                                        "' with dependency on currently created bean: " + ex.getMessage());
                            }
                            // Ignore: indicates a reference back to the bean we're trying to advise.
                            // We want to find advisors other than the currently created bean itself.
                            continue;
                        }
                    }
                    throw ex;
                }
            }
        }
    }
    return advisors;
}

findAdvisorBeans方法的的目的非常明確,就是找到容器中全部的Advisor

  1. 先嘗試從緩存中獲取Advisor集合
  2. 緩存中不存在魂迄,則執(zhí)行獲取邏輯
    1. 獲取到了所有類型為Advisor的Bean的名稱
    2. 根據(jù)獲取到的BeanName獲取Bean

為當(dāng)前Bean匹配合適的 Advisor

在以上的步驟中粗截,我們已經(jīng)拿到了容器中所有的Advisor對象集合,也就是說我們已經(jīng)拿到了容器中所有已配置的AOP切面捣炬。接下里的事情就是為當(dāng)前的目標(biāo)對象篩選出適合的Advisor集合熊昌,現(xiàn)在我們開始分析AbstractAdvisorAutoProxyCreator.classfindAdvisorsThatCanApply方法

protected List<Advisor> findAdvisorsThatCanApply(
        List<Advisor> candidateAdvisors, Class<?> beanClass, String beanName) {

    ProxyCreationContext.setCurrentProxiedBeanName(beanName);
    try {
        // 調(diào)用 AopUtils.findAdvisorsThatCanApply 為當(dāng)前Bean篩選合適的 Advisor
        return AopUtils.findAdvisorsThatCanApply(candidateAdvisors, beanClass);
    }
    finally {
        ProxyCreationContext.setCurrentProxiedBeanName(null);
    }
}

findAdvisorsThatCanApply方法里調(diào)用了工具類AOPUtilsfindAdvisorsThatCanApply方法

public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) {
    if (candidateAdvisors.isEmpty()) {
        return candidateAdvisors;
    }
    List<Advisor> eligibleAdvisors = new LinkedList<Advisor>();
    // 先找出 IntroductionAdvisor 類型的Advisor
    for (Advisor candidate : candidateAdvisors) {
        if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) {
            eligibleAdvisors.add(candidate);
        }
    }
    boolean hasIntroductions = !eligibleAdvisors.isEmpty();
    // 再從余下的Advisor中繼續(xù)匹配
    for (Advisor candidate : candidateAdvisors) {
        if (candidate instanceof IntroductionAdvisor) {
            // already processed
            continue;
        }
        if (canApply(candidate, clazz, hasIntroductions)) {
            eligibleAdvisors.add(candidate);
        }
    }
    return eligibleAdvisors;
}

以上代碼邏輯會先篩選出IntroductionAdvisor類型的Advisor,再篩選余下的其他Advisor

public static boolean canApply(Advisor advisor, Class<?> targetClass) {
    return canApply(advisor, targetClass, false);
}
public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
    if (advisor instanceof IntroductionAdvisor) {
        return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
    }
    else if (advisor instanceof PointcutAdvisor) {
        PointcutAdvisor pca = (PointcutAdvisor) advisor;
        return canApply(pca.getPointcut(), targetClass, hasIntroductions);
    }
    else {
        // It doesn't have a pointcut so we assume it applies.
        return true;
    }
}
public static boolean canApply(Pointcut pc, Class<?> targetClass, boolean hasIntroductions) {
    Assert.notNull(pc, "Pointcut must not be null");
    // 先判斷類型是否匹配
    if (!pc.getClassFilter().matches(targetClass)) {
        return false;
    }
    
    // 獲取切點方法匹配器
    MethodMatcher methodMatcher = pc.getMethodMatcher();
    if (methodMatcher == MethodMatcher.TRUE) {
        // No need to iterate the methods if we're matching any method anyway...
        return true;
    }

    IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
    if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
        introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
    }

    Set<Class<?>> classes = new LinkedHashSet<Class<?>>(ClassUtils.getAllInterfacesForClassAsSet(targetClass));
    classes.add(targetClass);
    for (Class<?> clazz : classes) {
        // 獲取當(dāng)前目標(biāo)class的所有方法
        Method[] methods = ReflectionUtils.getAllDeclaredMethods(clazz);
        for (Method method : methods) {
            // 調(diào)用 methodMatcher.matches 判斷當(dāng)前方法是否匹配該切點
            if ((introductionAwareMethodMatcher != null &&
                    introductionAwareMethodMatcher.matches(method, targetClass, hasIntroductions)) ||
                    methodMatcher.matches(method, targetClass)) {
                return true;
            }
        }
    }

    return false;
}

canApply是用于篩選核心方法湿酸,在上期分析連接點Pointcut接口時婿屹,我們知道Pointcut持有ClassFilterMethodMatcher對象,具體的篩選邏輯就是由它們完成的推溃,至于在它們具體的macthes方法是是如何實現(xiàn)篩選的昂利,這里我也沒有進(jìn)行過深入分析,感興趣的可以在其實現(xiàn)類AspectJExpressionPointcut.class中繼續(xù)查看

生成代理對象

在上文的分析結(jié)束后铁坎,我們已經(jīng)篩選出來了合適的 Advisor蜂奸,Advisor持有Advice(通知),接下來的操作就是根據(jù)我們配置的Advice為目標(biāo)對象創(chuàng)建代理對象了

protected Object createProxy(
        Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {

    if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
        AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
    }
    
    // 首先創(chuàng)建一個代理創(chuàng)建工廠類硬萍,之后的操作都是為此工廠配置屬性
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.copyFrom(this);
    // 判斷配置屬性proxy-target-class  是否等于False
    if (!proxyFactory.isProxyTargetClass()) {
        // 判斷目標(biāo) BeanDefinition是否配置preserveTargetClass 為 ture扩所,是的話配置CGLIB動態(tài)代理
        if (shouldProxyTargetClass(beanClass, beanName)) {
            proxyFactory.setProxyTargetClass(true);
        }
        else {
            // 設(shè)置目標(biāo)類接口到proxyFactory,如果沒有實現(xiàn)接口則使用CGLIB代理
            evaluateProxyInterfaces(beanClass, proxyFactory);
        }
    }

    Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
    // proxyFactory 設(shè)置 advisors
    proxyFactory.addAdvisors(advisors);
    // 設(shè)置目標(biāo)對象資源
    proxyFactory.setTargetSource(targetSource);
    customizeProxyFactory(proxyFactory);

    proxyFactory.setFrozen(this.freezeProxy);
    if (advisorsPreFiltered()) {
        proxyFactory.setPreFiltered(true);
    }
    
    // 創(chuàng)建代理
    return proxyFactory.getProxy(getProxyClassLoader());

創(chuàng)建代理對象前朴乖,會新建一個代理工廠創(chuàng)建類祖屏,并為此工廠類配置相關(guān)屬性助赞,例如proxy-target-class的配置,雖然默認(rèn)配置是false會使用JDK動態(tài)代理袁勺,但如果沒有實現(xiàn)接口嫉拐,也會自動設(shè)置proxy-target-classtrue使用CGLIB創(chuàng)建代理對象

完成ProxyFactory的配置之后,就可以通過它創(chuàng)建代理對象了

/**
 * 創(chuàng)建代理對象
 */
public Object getProxy(ClassLoader classLoader) {
    return createAopProxy().getProxy(classLoader);
}

調(diào)用父類ProxyCreatorSupportcreateAopProxy方法魁兼,獲取到代理創(chuàng)建工廠婉徘,工廠類(DefaultAopProxyFactory)是在父類的構(gòu)造方法中創(chuàng)建的

protected final synchronized AopProxy createAopProxy() {
    if (!this.active) {
        activate();
    }
    // 獲取代理創(chuàng)建工廠類 (DefaultAopProxyFactory.class)創(chuàng)建代理對象
    return getAopProxyFactory().createAopProxy(this);
}

最終在DefaultAopProxyFactory工廠類createAopProxy中創(chuàng)建代理對象

public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
    // 這里主要是判斷 proxy-target-class 屬性是否為true蒋伦。默認(rèn)是false
    if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
        Class<?> targetClass = config.getTargetClass();
        if (targetClass == null) {
            throw new AopConfigException("TargetSource cannot determine target class: " +
                    "Either an interface or a target is required for proxy creation.");
        }
        if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
            return new JdkDynamicAopProxy(config);
        }
        // 使用CGLIB動態(tài)代理
        return new ObjenesisCglibAopProxy(config);
    }
    else {
        // 默認(rèn)使用JDK動態(tài)打理
        return new JdkDynamicAopProxy(config);
    }
}

最終的代理的創(chuàng)建是在ObjenesisCglibAopProxyJdkDynamicAopProxy中完成的逮光,至于更具體的創(chuàng)建邏輯,因為這一系列的源碼分析只是為了能夠?qū)?code>AOP的整體邏輯有清晰的認(rèn)識弧岳,所以這里就不做更詳細(xì)的分析了化撕。

尾言

本篇文章分析了AOP代理創(chuàng)建的整個過程几晤,縱觀整篇文章,篇幅有限植阴,其中還有很多的點沒有詳細(xì)展開分析蟹瘾,只是粗略的分析了AOP代理的創(chuàng)建過程,慚愧 ... 掠手。分析有誤的地方憾朴,希望大家可以指出。

博客原文地址戳這里

Spring 系列

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末喷鸽,一起剝皮案震驚了整個濱河市众雷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌做祝,老刑警劉巖砾省,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異混槐,居然都是意外死亡编兄,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門声登,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狠鸳,“玉大人,你說我怎么就攤上這事捌刮∨龌停” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵绅作,是天一觀的道長芦圾。 經(jīng)常有香客問我,道長俄认,這世上最難降的妖魔是什么个少? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任洪乍,我火速辦了婚禮,結(jié)果婚禮上夜焦,老公的妹妹穿的比我還像新娘壳澳。我一直安慰自己,他們只是感情好茫经,可當(dāng)我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布巷波。 她就那樣靜靜地躺著,像睡著了一般卸伞。 火紅的嫁衣襯著肌膚如雪抹镊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天荤傲,我揣著相機(jī)與錄音垮耳,去河邊找鬼。 笑死遂黍,一個胖子當(dāng)著我的面吹牛终佛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播雾家,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼铃彰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了榜贴?” 一聲冷哼從身側(cè)響起豌研,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤妹田,失蹤者是張志新(化名)和其女友劉穎唬党,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鬼佣,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡驶拱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了晶衷。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蓝纲。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖晌纫,靈堂內(nèi)的尸體忽然破棺而出税迷,到底是詐尸還是另有隱情,我是刑警寧澤锹漱,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布箭养,位于F島的核電站,受9級特大地震影響哥牍,放射性物質(zhì)發(fā)生泄漏毕泌。R本人自食惡果不足惜喝检,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望撼泛。 院中可真熱鬧挠说,春花似錦、人聲如沸愿题。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽潘酗。三九已至撩炊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間崎脉,已是汗流浹背拧咳。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留囚灼,地道東北人骆膝。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像灶体,于是被迫代替她去往敵國和親阅签。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容