Spring構(gòu)造器注入源碼之determineCandidateConstructors

1凭疮、創(chuàng)建實例

AbstractAutowireCapableBeanFactory#doCreateBean

if (instanceWrapper == null) {  
    instanceWrapper = createBeanInstance(beanName, mbd, args);
}

2祷愉、確定構(gòu)造器

AbstractAutowireCapableBeanFactory#createBeanInstance

Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);

3躬窜、遍歷BeanPostProcessor

AbstractAutowireCapableBeanFactory#determineConstructorsFromBeanPostProcessors

if (beanClass != null && hasInstantiationAwareBeanPostProcessors()) {
            for (SmartInstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().smartInstantiationAware) {
                Constructor<?>[] ctors = bp.determineCandidateConstructors(beanClass, beanName);
                if (ctors != null) {
                    return ctors;
        }
    }
}

4捞高、選擇構(gòu)造器

AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors

public Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, final String beanName)
            throws BeanCreationException {

        // Let's check for lookup methods here...
        if (!this.lookupMethodsChecked.contains(beanName)) {
            if (AnnotationUtils.isCandidateClass(beanClass, Lookup.class)) {
                try {
                    Class<?> targetClass = beanClass;
                    do {
                        ReflectionUtils.doWithLocalMethods(targetClass, method -> {
                            Lookup lookup = method.getAnnotation(Lookup.class);
                            if (lookup != null) {
                                Assert.state(this.beanFactory != null, "No BeanFactory available");
                                LookupOverride override = new LookupOverride(method, lookup.value());
                                try {
                                    RootBeanDefinition mbd = (RootBeanDefinition)
                                            this.beanFactory.getMergedBeanDefinition(beanName);
                                    mbd.getMethodOverrides().addOverride(override);
                                }
                                catch (NoSuchBeanDefinitionException ex) {
                                    throw new BeanCreationException(beanName,
                                            "Cannot apply @Lookup to beans without corresponding bean definition");
                                }
                            }
                        });
                        targetClass = targetClass.getSuperclass();
                    }
                    while (targetClass != null && targetClass != Object.class);

                }
                catch (IllegalStateException ex) {
                    throw new BeanCreationException(beanName, "Lookup method resolution failed", ex);
                }
            }
            this.lookupMethodsChecked.add(beanName);
        }

        // Quick check on the concurrent map first, with minimal locking.

        // 1、先從candidateConstructorsCache緩存獲取候選構(gòu)造器
        Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);

        // 2甸私、緩存沒有則重新獲取
        if (candidateConstructors == null) {
            // Fully synchronized resolution now...
            synchronized (this.candidateConstructorsCache) {
                candidateConstructors = this.candidateConstructorsCache.get(beanClass);
                if (candidateConstructors == null) {
                    Constructor<?>[] rawCandidates;
                    // 2.1 獲取所有的構(gòu)造器rawCandidates
                    try {
                        rawCandidates = beanClass.getDeclaredConstructors();
                    }
                    catch (Throwable ex) {
                        throw new BeanCreationException(beanName,
                                "Resolution of declared constructors on bean Class [" + beanClass.getName() +
                                "] from ClassLoader [" + beanClass.getClassLoader() + "] failed", ex);
                    }
                    List<Constructor<?>> candidates = new ArrayList<>(rawCandidates.length);
                    Constructor<?> requiredConstructor = null;
                    Constructor<?> defaultConstructor = null;
                    Constructor<?> primaryConstructor = BeanUtils.findPrimaryConstructor(beanClass);
                    int nonSyntheticConstructors = 0;
                    // 2.2 遍歷所有構(gòu)造器rawCandidates诚些,將添加@Autowired的注解添加至candidates,將無參構(gòu)造賦給defaultConstructor
                    for (Constructor<?> candidate : rawCandidates) {
                        if (!candidate.isSynthetic()) {
                            nonSyntheticConstructors++;
                        }
                        else if (primaryConstructor != null) {
                            continue;
                        }
                        // 2.2.1 獲取有@Autowired注解的構(gòu)造器皇型,包括父類
                        MergedAnnotation<?> ann = findAutowiredAnnotation(candidate);
                        if (ann == null) {
                            Class<?> userClass = ClassUtils.getUserClass(beanClass);
                            if (userClass != beanClass) {
                                try {
                                    Constructor<?> superCtor =
                                            userClass.getDeclaredConstructor(candidate.getParameterTypes());
                                    ann = findAutowiredAnnotation(superCtor);
                                }
                                catch (NoSuchMethodException ex) {
                                    // Simply proceed, no equivalent superclass constructor found...
                                }
                            }
                        }
                        // 2.2.2 如果有@Autowired注解诬烹,最后的操作是candidates.add(candidate),但如果出現(xiàn)兩種情況會拋出異常
                        //      a、如果之前有遍歷過@Autowired的構(gòu)造器弃鸦,即requiredConstructor不為空
                        //      b绞吁、如果當前的@Autowired的required屬性為true,不管之前遍歷過的@Autowired屬性是true還是false
                        if (ann != null) {
                            if (requiredConstructor != null) {
                                throw new BeanCreationException(beanName,
                                        "Invalid autowire-marked constructor: " + candidate +
                                        ". Found constructor with 'required' Autowired annotation already: " +
                                        requiredConstructor);
                            }
                            boolean required = determineRequiredStatus(ann);
                            if (required) {
                                if (!candidates.isEmpty()) {
                                    throw new BeanCreationException(beanName,
                                            "Invalid autowire-marked constructors: " + candidates +
                                            ". Found constructor with 'required' Autowired annotation: " +
                                            candidate);
                                }
                                requiredConstructor = candidate;
                            }
                            candidates.add(candidate);
                        }
                        // // 2.2.3 獲取無參構(gòu)造函數(shù)
                        else if (candidate.getParameterCount() == 0) {
                            defaultConstructor = candidate;
                        }
                    }
                    // 2.3 如果@Autowired注解的構(gòu)造函數(shù)不為空唬格,且不包含required屬性為true家破,那么把無參構(gòu)造函數(shù)加至candidates
                    // 將candidates轉(zhuǎn)成candidateConstructors列表
                    if (!candidates.isEmpty()) {
                        // Add default constructor to list of optional constructors, as fallback.
                        if (requiredConstructor == null) {
                            if (defaultConstructor != null) {
                                candidates.add(defaultConstructor);
                            }
                            else if (candidates.size() == 1 && logger.isInfoEnabled()) {
                                logger.info("Inconsistent constructor declaration on bean with name '" + beanName +
                                        "': single autowire-marked constructor flagged as optional - " +
                                        "this constructor is effectively required since there is no " +
                                        "default constructor to fall back to: " + candidates.get(0));
                            }
                        }
                        candidateConstructors = candidates.toArray(new Constructor<?>[0]);
                    }
                    // 2.4 如果構(gòu)造函數(shù)只有一個,且有參數(shù)购岗,那么將該構(gòu)造函數(shù)賦給candidateConstructors
                    else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
                        candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
                    }
                    else if (nonSyntheticConstructors == 2 && primaryConstructor != null &&
                            defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
                        candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
                    }
                    else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
                        candidateConstructors = new Constructor<?>[] {primaryConstructor};
                    }
                    // 2.5 返回0個數(shù)的candidateConstructors
                    else {
                        candidateConstructors = new Constructor<?>[0];
                    }
                    this.candidateConstructorsCache.put(beanClass, candidateConstructors);
                }
            }
        }
        // 3汰聋、有候選構(gòu)造器則返回,沒有返回null
        return (candidateConstructors.length > 0 ? candidateConstructors : null);
    }

5喊积、總結(jié)

  • 構(gòu)造器的選擇主要是從determineCandidateConstructors代碼中候選構(gòu)造器主要從
Constructor<?>[] candidateConstructors = this.candidateConstructorsCache.get(beanClass);

后面開始的

  • 獲取到所有的帶@Autowired注解的構(gòu)造函數(shù)烹困,如果有一個@Autowired注解的required屬性為true,那么就不能再有帶@Autowired注解(不管required屬性是true還是false)的構(gòu)造函數(shù)乾吻,否則會報錯
  • 帶有@Autowire(required=false)的構(gòu)造函數(shù)可以有多個

最后候選構(gòu)造返回的邏輯如下

  • 如果有@Autowired的構(gòu)造函數(shù)髓梅,那么不需要將無參構(gòu)造加入候選列表拟蜻,并返回
  • 如果@Autowired的required屬性都為false,需將無參構(gòu)造加入候選列表枯饿,并返回
  • 如果沒有@Autowired注解的構(gòu)造函數(shù)酝锅,只有一個有參構(gòu)造,那么將該構(gòu)造函數(shù)返回
 // 2.4 如果構(gòu)造函數(shù)只有一個奢方,且有參數(shù)搔扁,那么將該構(gòu)造函數(shù)賦給candidateConstructors
                    else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
                        candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
                    }
  • 不滿足上述條件的候選構(gòu)造參數(shù)為0,最終會用無參構(gòu)造實例化袱巨,當然如下的判斷與kotlin相關(guān),在此先忽略
  else if (nonSyntheticConstructors == 2 && primaryConstructor != null &&
                            defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
                        candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
                    }
                    else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
                        candidateConstructors = new Constructor<?>[] {primaryConstructor};
                    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末碳抄,一起剝皮案震驚了整個濱河市愉老,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌剖效,老刑警劉巖嫉入,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異璧尸,居然都是意外死亡咒林,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門爷光,熙熙樓的掌柜王于貴愁眉苦臉地迎上來垫竞,“玉大人,你說我怎么就攤上這事蛀序』兜桑” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵徐裸,是天一觀的道長遣鼓。 經(jīng)常有香客問我,道長重贺,這世上最難降的妖魔是什么骑祟? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮气笙,結(jié)果婚禮上次企,老公的妹妹穿的比我還像新娘。我一直安慰自己潜圃,他們只是感情好抒巢,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著秉犹,像睡著了一般蛉谜。 火紅的嫁衣襯著肌膚如雪稚晚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天型诚,我揣著相機與錄音客燕,去河邊找鬼。 笑死狰贯,一個胖子當著我的面吹牛也搓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播涵紊,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼傍妒,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了摸柄?” 一聲冷哼從身側(cè)響起颤练,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎驱负,沒想到半個月后嗦玖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡跃脊,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年宇挫,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片酪术。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡器瘪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绘雁,到底是詐尸還是另有隱情娱局,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布咧七,位于F島的核電站衰齐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏继阻。R本人自食惡果不足惜耻涛,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望瘟檩。 院中可真熱鬧抹缕,春花似錦、人聲如沸墨辛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至奏赘,卻和暖如春寥闪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背磨淌。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工疲憋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人梁只。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓缚柳,卻偏偏與公主長得像,于是被迫代替她去往敵國和親搪锣。 傳聞我的和親對象是個殘疾皇子秋忙,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

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