Spring源碼分析之IOC的三種常見用法及源碼實(shí)現(xiàn)(三)

上篇文章我們分析了AnnotationConfigApplicationContext的構(gòu)造器里refresh方法里的invokeBeanFactoryPostProcessors肌幽,了解了@Component和@Bean的原理等,invokeBeanFactoryPostProcessors還沒分析完抓半,后面還有部分代碼喂急,繼續(xù)看

refresh里的invokeBeanFactoryPostProcessors剩余代碼

首先看看整體

public static void invokeBeanFactoryPostProcessors(
            ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

        // Invoke BeanDefinitionRegistryPostProcessors first, if any.
        Set<String> processedBeans = new HashSet<String>();

        if (beanFactory instanceof BeanDefinitionRegistry) {
            BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
            List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
            List<BeanDefinitionRegistryPostProcessor> registryPostProcessors =
                    new LinkedList<BeanDefinitionRegistryPostProcessor>();

            for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
                if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                    BeanDefinitionRegistryPostProcessor registryPostProcessor =
                            (BeanDefinitionRegistryPostProcessor) postProcessor;
                    registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
                    registryPostProcessors.add(registryPostProcessor);
                }
                else {
                    regularPostProcessors.add(postProcessor);
                }
            }

            // Do not initialize FactoryBeans here: We need to leave all regular beans
            // uninitialized to let the bean factory post-processors apply to them!
            // Separate between BeanDefinitionRegistryPostProcessors that implement
            // PriorityOrdered, Ordered, and the rest.
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);

            // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
            List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
            for (String ppName : postProcessorNames) {
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
            registryPostProcessors.addAll(priorityOrderedPostProcessors);
            invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);

            // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            List<BeanDefinitionRegistryPostProcessor> orderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
            for (String ppName : postProcessorNames) {
                if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                    orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }
            sortPostProcessors(beanFactory, orderedPostProcessors);
            registryPostProcessors.addAll(orderedPostProcessors);
            invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);

            // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
            boolean reiterate = true;
            while (reiterate) {
                reiterate = false;
                postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
                for (String ppName : postProcessorNames) {
                    if (!processedBeans.contains(ppName)) {
                        BeanDefinitionRegistryPostProcessor pp = beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
                        registryPostProcessors.add(pp);
                        processedBeans.add(ppName);
                        pp.postProcessBeanDefinitionRegistry(registry);
                        reiterate = true;
                    }
                }
            }

            // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
            invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory);
            invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
        }

        else {
            // Invoke factory processors registered with the context instance.
            invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
        }

        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let the bean factory post-processors apply to them!
        String[] postProcessorNames =
                beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

        // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
        // Ordered, and the rest.
        List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
        List<String> orderedPostProcessorNames = new ArrayList<String>();
        List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
        for (String ppName : postProcessorNames) {
            if (processedBeans.contains(ppName)) {
                // skip - already processed in first phase above
            }
            else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
            }
            else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
                orderedPostProcessorNames.add(ppName);
            }
            else {
                nonOrderedPostProcessorNames.add(ppName);
            }
        }

        // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
        sortPostProcessors(beanFactory, priorityOrderedPostProcessors);
        invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

        // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
        List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
        for (String postProcessorName : orderedPostProcessorNames) {
            orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
        }
        sortPostProcessors(beanFactory, orderedPostProcessors);
        invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

        // Finally, invoke all other BeanFactoryPostProcessors.
        List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
        for (String postProcessorName : nonOrderedPostProcessorNames) {
            nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
        }
        invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

        // Clear cached merged bean definitions since the post-processors might have
        // modified the original metadata, e.g. replacing placeholders in values...
        beanFactory.clearMetadataCache();
    }

上次是講了里面的invokeBeanFactoryPostProcessors,里面有很多次invokeBeanFactoryPostProcessors調(diào)用笛求,后面這么多重復(fù)調(diào)用是干嘛呢廊移?

其實(shí)在debug的時(shí)候并沒有執(zhí)行,這里體現(xiàn)的spring的一個(gè)理念 開放接口探入,只要當(dāng)我們覆寫了它的接口的時(shí)候這些后面的invokeBeanFactoryPostProcessors就會(huì)執(zhí)行狡孔,這也是為什么這里創(chuàng)建了好幾個(gè)LinkedList把不同的加到不同的LinkedList中,這樣分類就是為了去區(qū)分你覆寫的接口然后去調(diào)用你寫的接口里的代碼蜂嗽。

具體的說:看里面注釋:

// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
            List<BeanDefinitionRegistryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
            for (String ppName : postProcessorNames) {
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                }
            }

First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.

翻譯:首先苗膝,調(diào)用實(shí)現(xiàn)了PriorityOrdered接口的BeanDefinitionRegistryPostProcessors。

// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.

翻譯://接下來植旧,調(diào)用實(shí)現(xiàn)Ordered接口的BeanDefinitionRegistryPostProcessors辱揭。

總的來說离唐,區(qū)分有沒有實(shí)現(xiàn)Ordered接口然后再根據(jù)排序調(diào)用。

從getBean()說起

getBean方法源碼如下:

    public Object getBean(String name) throws BeansException {
        this.assertBeanFactoryActive();
        return this.getBeanFactory().getBean(name);
    }

第一行只是判斷beanFactory是否激活了问窃,里面只是if else判斷一下 關(guān)閉了咋拋異常亥鬓。我們看重點(diǎn)第二局的getBean源碼:

    public Object getBean(String name) throws BeansException {
        return doGetBean(name, null, null, false);
    }

一看到doXXX就知道這是spring的習(xí)慣doXXX就會(huì)有真正的主邏輯了,進(jìn)去:

    protected <T> T doGetBean(
            final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)
            throws BeansException {

        final String beanName = transformedBeanName(name);
        Object bean;

        // Eagerly check singleton cache for manually registered singletons.
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null && args == null) {
            if (logger.isDebugEnabled()) {
                if (isSingletonCurrentlyInCreation(beanName)) {
                    logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
                            "' that is not fully initialized yet - a consequence of a circular reference");
                }
                else {
                    logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
                }
            }
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }

        else {
            // Fail if we're already creating this bean instance:
            // We're assumably within a circular reference.
            if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }

            // Check if bean definition exists in this factory.
            BeanFactory parentBeanFactory = getParentBeanFactory();
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                // Not found -> check parent.
                String nameToLookup = originalBeanName(name);
                if (args != null) {
                    // Delegation to parent with explicit args.
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                }
                else {
                    // No args -> delegate to standard getBean method.
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
            }

            if (!typeCheckOnly) {
                markBeanAsCreated(beanName);
            }

            try {
                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                checkMergedBeanDefinition(mbd, beanName, args);

                // Guarantee initialization of beans that the current bean depends on.
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {
                    for (String dep : dependsOn) {
                        if (isDependent(beanName, dep)) {
                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
                        }
                        registerDependentBean(dep, beanName);
                        getBean(dep);
                    }
                }

                // Create bean instance.
                if (mbd.isSingleton()) {
                    sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
                        @Override
                        public Object getObject() throws BeansException {
                            try {
                                return createBean(beanName, mbd, args);
                            }
                            catch (BeansException ex) {
                                // Explicitly remove instance from singleton cache: It might have been put there
                                // eagerly by the creation process, to allow for circular reference resolution.
                                // Also remove any beans that received a temporary reference to the bean.
                                destroySingleton(beanName);
                                throw ex;
                            }
                        }
                    });
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }

                else if (mbd.isPrototype()) {
                    // It's a prototype -> create a new instance.
                    Object prototypeInstance = null;
                    try {
                        beforePrototypeCreation(beanName);
                        prototypeInstance = createBean(beanName, mbd, args);
                    }
                    finally {
                        afterPrototypeCreation(beanName);
                    }
                    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                }

                else {
                    String scopeName = mbd.getScope();
                    final Scope scope = this.scopes.get(scopeName);
                    if (scope == null) {
                        throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
                    }
                    try {
                        Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
                            @Override
                            public Object getObject() throws BeansException {
                                beforePrototypeCreation(beanName);
                                try {
                                    return createBean(beanName, mbd, args);
                                }
                                finally {
                                    afterPrototypeCreation(beanName);
                                }
                            }
                        });
                        bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                    }
                    catch (IllegalStateException ex) {
                        throw new BeanCreationException(beanName,
                                "Scope '" + scopeName + "' is not active for the current thread; consider " +
                                "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                                ex);
                    }
                }
            }
            catch (BeansException ex) {
                cleanupAfterBeanCreationFailure(beanName);
                throw ex;
            }
        }

        // Check if required type matches the type of the actual bean instance.
        if (requiredType != null && bean != null && !requiredType.isInstance(bean)) {
            try {
                return getTypeConverter().convertIfNecessary(bean, requiredType);
            }
            catch (TypeMismatchException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to convert bean '" + name + "' to required type '" +
                            ClassUtils.getQualifiedName(requiredType) + "'", ex);
                }
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
        }
        return (T) bean;
    }

doGetBean的transformedBeanName

超級(jí)長(zhǎng)的一個(gè)方法域庇,慢慢分析嵌戈,第一行是transformedBeanName方法獲取名字,看看它是怎么獲取名字的听皿,點(diǎn)開源碼:

    protected String transformedBeanName(String name) {
        return canonicalName(BeanFactoryUtils.transformedBeanName(name));
    }

看來是兩次轉(zhuǎn)換熟呛,先看第一層BeanFactoryUtils.transformedBeanName(name):

    public static String transformedBeanName(String name) {
        Assert.notNull(name, "'name' must not be null");
        String beanName = name;
        while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
            beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
        }
        return beanName;
    }

這個(gè)BeanFactory.FACTORY_BEAN_PREFIX實(shí)際上是個(gè)"&"符號(hào),這里就是判斷下是不是工廠Bean写穴,把利用while循環(huán)把開頭的&符號(hào)都去掉惰拱。

然后我們看第二層名字轉(zhuǎn)換:

    public String canonicalName(String name) {
        String canonicalName = name;

        String resolvedName;
        do {
            resolvedName = (String)this.aliasMap.get(canonicalName);
            if (resolvedName != null) {
                canonicalName = resolvedName;
            }
        } while(resolvedName != null);

        return canonicalName;
    }

這里看樣子是從aliasMap里取值,alias存著別名對(duì)應(yīng)的真正容器的名字啊送。然而你會(huì)發(fā)現(xiàn)這里居然是個(gè)do while循環(huán)偿短,這是為什么?因?yàn)榭赡艽嬖诙嘀貏e名的情況馋没,需要不斷的取別名的真名昔逗,真名又對(duì)應(yīng)的真名...取到最終的真名。

ok去bean名字看完了篷朵,接下來看doGetBean那么長(zhǎng)那么長(zhǎng)的后面的代碼勾怒,接下來來看下?lián)f是SpringIOC中最核心的方法代碼。

doGetBean的getSingleton

查看源碼:

    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
            synchronized (this.singletonObjects) {
                singletonObject = this.earlySingletonObjects.get(beanName);
                if (singletonObject == null && allowEarlyReference) {
                    ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
                    if (singletonFactory != null) {
                        singletonObject = singletonFactory.getObject();
                        this.earlySingletonObjects.put(beanName, singletonObject);
                        this.singletonFactories.remove(beanName);
                    }
                }
            }
        }
        return (singletonObject != NULL_OBJECT ? singletonObject : null);
    }

Ok声旺,這里就是IOC大名鼎鼎的多級(jí)緩存(解決循環(huán)依賴)了笔链,首先第一句是從singletonObjects里當(dāng)map直接取,其實(shí)singletonObjects就是叫單例緩存池腮猖,是一個(gè)ConcurrentHashMap鉴扫。

實(shí)際上當(dāng)我第一次打斷點(diǎn)調(diào)試的時(shí)候,始終直接就從singletonObjects中取到了想要的對(duì)象澈缺,后面的if判斷就不進(jìn)去坪创,最后return了 然后就結(jié)束了。

然而多級(jí)緩存不會(huì)這么簡(jiǎn)單就拿到了姐赡,我們?yōu)槭裁粗苯幽苣玫侥乩吃ぃ科鋵?shí)是已經(jīng)調(diào)用過一次getBean了,調(diào)用過了的就直接緩存在singletonObjects了项滑,所以我們這次可以直接拿到依沮,可以研究下第一次是什么時(shí)候創(chuàng)建在里面的,其實(shí)是在上篇文章的構(gòu)造器的refresh方法中。

我們可以直接在getBean里打斷點(diǎn)而不是從最簡(jiǎn)單那兩句慢慢跳進(jìn)去悉抵,就可以停留在第一次getBean的時(shí)候了肩狂。實(shí)際上后面那個(gè)if判斷還是進(jìn)不去,if還有第二個(gè)條件必須正在創(chuàng)建時(shí)才進(jìn)去姥饰,此時(shí)return null傻谁,ok,這才是第一次執(zhí)行的樣子列粪。

然后就返回上層那個(gè)很長(zhǎng)的doGetBean方法了

回到doGetBean

由于返回的是Null审磁,所以我們進(jìn)后面的else了

else {
            // Fail if we're already creating this bean instance:
            // We're assumably within a circular reference.
            if (isPrototypeCurrentlyInCreation(beanName)) {
                throw new BeanCurrentlyInCreationException(beanName);
            }
            ....

這里是對(duì)原型模式發(fā)生循環(huán)依賴的異常拋出,因?yàn)檫@里現(xiàn)在是準(zhǔn)備創(chuàng)建岂座,然后這個(gè)判斷發(fā)現(xiàn)也同時(shí)在創(chuàng)建态蒂,這明顯發(fā)生了循環(huán)依賴,于是拋出费什。

至于為什么原型模式不能解決循環(huán)钾恢,因?yàn)樵湍J剑ㄒ簿褪嵌嗬]進(jìn)行多級(jí)緩存。

后面代碼:

            // Check if bean definition exists in this factory.
            BeanFactory parentBeanFactory = getParentBeanFactory();
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                // Not found -> check parent.
                String nameToLookup = originalBeanName(name);
                if (args != null) {
                    // Delegation to parent with explicit args.
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                }
                else {
                    // No args -> delegate to standard getBean method.
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
            }

            if (!typeCheckOnly) {
                markBeanAsCreated(beanName);
            }

這里判斷是不是有父級(jí)bean工廠鸳址,有則從上級(jí)的getBean取瘩蚪。后面就是判斷創(chuàng)建的標(biāo)記,將指定的bean標(biāo)記為已經(jīng)創(chuàng)建(或?qū)⒁獎(jiǎng)?chuàng)建)稿黍。

繼續(xù)doGetBean往后看:

                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                checkMergedBeanDefinition(mbd, beanName, args);

在以前xml配置bean的時(shí)候可以加一個(gè)abstract屬性疹瘦,表達(dá)這個(gè)bean是一個(gè)父類,然后配置別的bean的時(shí)候可以繼承自它巡球,實(shí)現(xiàn)復(fù)用言沐。那么這里的這個(gè)代碼getMergedLocalBeanDefinition,其實(shí)就是獲取是不是有父類酣栈,然后把他自己和父類的信息合并到一起成RootBeanDefinition并返回险胰。后面這個(gè)checkMergedBeanDefinition,打開源碼:

    protected void checkMergedBeanDefinition(RootBeanDefinition mbd, String beanName, Object[] args)
            throws BeanDefinitionStoreException {

        if (mbd.isAbstract()) {
            throw new BeanIsAbstractException(beanName);
        }
    }

其實(shí)就是檢查是不是合并好了矿筝,如果還存在abstract的就拋出異常鸯乃。繼續(xù)doGetBean往后看:

// Guarantee initialization of beans that the current bean depends on.
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
    for (String dep : dependsOn) {
        if (isDependent(beanName, dep)) {
            throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                            "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
        }
        registerDependentBean(dep, beanName);
        getBean(dep);
    }
}

這個(gè)是和@DependsOn注解有關(guān)的,有時(shí)候有些的bean初始化之前的得先初始化另外一個(gè)Bean跋涣,就需要用到這個(gè)@DependsOn注解。

而這里這個(gè)getDependsOn就是獲取需要先加載的bean鸟悴,然后遍歷這些bean陈辱,其中那個(gè)if判斷就是檢查它依賴的bean會(huì)不會(huì)是它自己,這樣就造成循環(huán)细诸,就拋出異常沛贪。

后面的registerDependentBean是注冊(cè)依賴關(guān)系,就是把哪些依賴我 我依賴哪些,都存到map里去

再后面的getBean就是把依賴的bean初始化利赋,這段就講完了水评,再在doGetBean里往后看:

if (mbd.isSingleton()) {
    sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
        @Override
        public Object getObject() throws BeansException {
            try {
                return createBean(beanName, mbd, args);
            }
            catch (BeansException ex) {
                // Explicitly remove instance from singleton cache: It might have been put there
                // eagerly by the creation process, to allow for circular reference resolution.
                // Also remove any beans that received a temporary reference to the bean.
                destroySingleton(beanName);
                throw ex;
            }
        }
    });
    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}

else if (mbd.isPrototype()) {
    // It's a prototype -> create a new instance.
    Object prototypeInstance = null;
    try {
        beforePrototypeCreation(beanName);
        prototypeInstance = createBean(beanName, mbd, args);
    }
    finally {
        afterPrototypeCreation(beanName);
    }
    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}

else {
    String scopeName = mbd.getScope();
    final Scope scope = this.scopes.get(scopeName);
    if (scope == null) {
        throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
    }
    try {
        Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
            @Override
            public Object getObject() throws BeansException {
                beforePrototypeCreation(beanName);
                try {
                    return createBean(beanName, mbd, args);
                }
                finally {
                    afterPrototypeCreation(beanName);
                }
            }
        });
        bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
    }
    catch (IllegalStateException ex) {
        throw new BeanCreationException(beanName,
                "Scope '" + scopeName + "' is not active for the current thread; consider " +
                        "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                ex);
    }
}

看樣子是根據(jù)bean的類型分三種情況,單例媚送、原型中燥、其它 進(jìn)行獲取。首先來看看單例的情況:

doGetBean的創(chuàng)建單例Bean的情況

if (mbd.isSingleton()) {
    sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
        @Override
        public Object getObject() throws BeansException {
            try {
                return createBean(beanName, mbd, args);
            }
            catch (BeansException ex) {
                // Explicitly remove instance from singleton cache: It might have been put there
                // eagerly by the creation process, to allow for circular reference resolution.
                // Also remove any beans that received a temporary reference to the bean.
                destroySingleton(beanName);
                throw ex;
            }
        }
    });
    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}

打開第一行的getSingleton方法源碼(getSingleton有多個(gè)重載實(shí)現(xiàn)):

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
    Assert.notNull(beanName, "'beanName' must not be null");
    synchronized (this.singletonObjects) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null) {
            if (this.singletonsCurrentlyInDestruction) {
                throw new BeanCreationNotAllowedException(beanName,
                        "Singleton bean creation not allowed while singletons of this factory are in destruction " +
                                "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
            }
            beforeSingletonCreation(beanName);
            boolean newSingleton = false;
            boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
            if (recordSuppressedExceptions) {
                this.suppressedExceptions = new LinkedHashSet<Exception>();
            }
            try {
                singletonObject = singletonFactory.getObject();
                newSingleton = true;
            }
            catch (IllegalStateException ex) {
                // Has the singleton object implicitly appeared in the meantime ->
                // if yes, proceed with it since the exception indicates that state.
                singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {
                    throw ex;
                }
            }
            catch (BeanCreationException ex) {
                if (recordSuppressedExceptions) {
                    for (Exception suppressedException : this.suppressedExceptions) {
                        ex.addRelatedCause(suppressedException);
                    }
                }
                throw ex;
            }
            finally {
                if (recordSuppressedExceptions) {
                    this.suppressedExceptions = null;
                }
                afterSingletonCreation(beanName);
            }
            if (newSingleton) {
                addSingleton(beanName, singletonObject);
            }
        }
        return (singletonObject != NULL_OBJECT ? singletonObject : null);
    }
}

這里還是先去單例緩存池去取塘偎,取得到直接返回疗涉,取不到就進(jìn)if判斷了。if里的第一個(gè)真正執(zhí)行的代碼是beforeSingletonCreation(beanName);可以點(diǎn)進(jìn)去查看:

protected void beforeSingletonCreation(String beanName) {
    if (!this.inCreationCheckExclusions.contains(beanName) && !this.singletonsCurrentlyInCreation.add(beanName)) {
        throw new BeanCurrentlyInCreationException(beanName);
    }
}

其實(shí)就是一個(gè)判斷吟秩,巧用了if的&&咱扣,主要是就是把beanName加到singletonsCurrentlyInCreation做個(gè)記號(hào),告訴這里正在創(chuàng)建涵防。之前第一次訪問doGetBean里第一次訪問getSingleton的時(shí)候就是因?yàn)檫@個(gè)記號(hào)闹伪,沒有進(jìn)if判斷。

后面就是開始創(chuàng)建bean壮池,拿了個(gè)標(biāo)記變量并且調(diào)用了傳參進(jìn)來的的singletonFactory的getObject方法偏瓤。最后創(chuàng)建好了的話加入到單例緩存池(addSingleton(beanName, singletonObject);)和注冊(cè)單例,都是一個(gè)map put就好了火窒。這里主要就是getObject方法硼补。就是之前這:

if (mbd.isSingleton()) {
    sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
        @Override
        public Object getObject() throws BeansException {
            try {
                return createBean(beanName, mbd, args);
            }
            catch (BeansException ex) {
                // Explicitly remove instance from singleton cache: It might have been put there
                // eagerly by the creation process, to allow for circular reference resolution.
                // Also remove any beans that received a temporary reference to the bean.
                destroySingleton(beanName);
                throw ex;
            }
        }
    });
    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}

可以看出實(shí)際上是調(diào)用的createBean

doGetBean的createBean

打開源碼:

/**
     * Central method of this class: creates a bean instance,
     * populates the bean instance, applies post-processors, etc.
     * @see #doCreateBean
     */
    @Override
    protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
        if (logger.isDebugEnabled()) {
            logger.debug("Creating instance of bean '" + beanName + "'");
        }
        RootBeanDefinition mbdToUse = mbd;

        // Make sure bean class is actually resolved at this point, and
        // clone the bean definition in case of a dynamically resolved Class
        // which cannot be stored in the shared merged bean definition.
        Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
        if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
            mbdToUse = new RootBeanDefinition(mbd);
            mbdToUse.setBeanClass(resolvedClass);
        }

        // Prepare method overrides.
        try {
            mbdToUse.prepareMethodOverrides();
        }
        catch (BeanDefinitionValidationException ex) {
            throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
                    beanName, "Validation of method overrides failed", ex);
        }

        try {
            // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
            Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
            if (bean != null) {
                return bean;
            }
        }
        catch (Throwable ex) {
            throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
                    "BeanPostProcessor before instantiation of bean failed", ex);
        }

        Object beanInstance = doCreateBean(beanName, mbdToUse, args);
        if (logger.isDebugEnabled()) {
            logger.debug("Finished creating instance of bean '" + beanName + "'");
        }
        return beanInstance;
    }

首先是從整合的RootBeanDefinition中解析出Class類型,然后是準(zhǔn)備方法覆寫熏矿。

后面這行代碼就比較重要了:

//Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.返回代理對(duì)象已骇,但是在IOC這里時(shí)候暫時(shí)沒有用,之后AOP會(huì)是核心票编。
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);

這里是AOP的核心缔逛,留給之后文章講。是在有切面的時(shí)候切面找出來并緩存李命。

繼續(xù)往后看片拍,看到了doXXX,這又是到了spring真正邏輯操作的地方了昔榴。

createBean的doCreateBean

調(diào)用這個(gè)方法時(shí)把beanNaem和RootBeanDefinition等傳進(jìn)去了

打開源碼:

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args)
    throws BeanCreationException {

    // Instantiate the bean.
    BeanWrapper instanceWrapper = null;
    if (mbd.isSingleton()) {
        instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
    }
    if (instanceWrapper == null) {
        instanceWrapper = createBeanInstance(beanName, mbd, args);
    }
    final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);
    Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);
    mbd.resolvedTargetType = beanType;

    // Allow post-processors to modify the merged bean definition.
    synchronized (mbd.postProcessingLock) {
        if (!mbd.postProcessed) {
            try {
                applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
            }
            catch (Throwable ex) {
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Post-processing of merged bean definition failed", ex);
            }
            mbd.postProcessed = true;
        }
    }

    // Eagerly cache singletons to be able to resolve circular references
    // even when triggered by lifecycle interfaces like BeanFactoryAware.
    boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
            isSingletonCurrentlyInCreation(beanName));
    if (earlySingletonExposure) {
        if (logger.isDebugEnabled()) {
            logger.debug("Eagerly caching bean '" + beanName +
                    "' to allow for resolving potential circular references");
        }
        addSingletonFactory(beanName, new ObjectFactory<Object>() {
            @Override
            public Object getObject() throws BeansException {
                return getEarlyBeanReference(beanName, mbd, bean);
            }
        });
    }

    // Initialize the bean instance.
    Object exposedObject = bean;
    try {
        populateBean(beanName, mbd, instanceWrapper);
        if (exposedObject != null) {
            exposedObject = initializeBean(beanName, exposedObject, mbd);
        }
    }
    catch (Throwable ex) {
        if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
            throw (BeanCreationException) ex;
        }
        else {
            throw new BeanCreationException(
                    mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
        }
    }

    if (earlySingletonExposure) {
        Object earlySingletonReference = getSingleton(beanName, false);
        if (earlySingletonReference != null) {
            if (exposedObject == bean) {
                exposedObject = earlySingletonReference;
            }
            else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
                String[] dependentBeans = getDependentBeans(beanName);
                Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
                for (String dependentBean : dependentBeans) {
                    if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                        actualDependentBeans.add(dependentBean);
                    }
                }
                if (!actualDependentBeans.isEmpty()) {
                    throw new BeanCurrentlyInCreationException(beanName,
                            ".....);
                }
            }
        }
    }

    // Register bean as disposable.
    try {
        registerDisposableBeanIfNecessary(beanName, bean, mbd);
    }
    catch (BeanDefinitionValidationException ex) {
        throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
    }

    return exposedObject;
}

其中辛藻,調(diào)用instantiateUsingFactoryMethod是基于工廠方法去創(chuàng)建,autowireConstructor是根據(jù)構(gòu)造器去創(chuàng)建的 基于反射的技術(shù)互订。

往后看:

// 緩存早期單例吱肌,以便即使在諸如BeanFactoryAware之類的生命周期接口觸發(fā)時(shí)也能夠解析循環(huán)引用。
boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
        isSingletonCurrentlyInCreation(beanName));
if (earlySingletonExposure) {
    if (logger.isDebugEnabled()) {
        logger.debug("Eagerly caching bean '" + beanName +
                "' to allow for resolving potential circular references");
    }
    addSingletonFactory(beanName, new ObjectFactory<Object>() {
        @Override
        public Object getObject() throws BeansException {
            return getEarlyBeanReference(beanName, mbd, bean);
        }
    });
}

// 初始化bean實(shí)例.
Object exposedObject = bean;
try {
    populateBean(beanName, mbd, instanceWrapper);
    if (exposedObject != null) {
        exposedObject = initializeBean(beanName, exposedObject, mbd);
    }
}
catch (Throwable ex) {
    if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
        throw (BeanCreationException) ex;
    }
    else {
        throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
    }
}

這里在滿足三個(gè)條件的情況下進(jìn)if仰禽,調(diào)用addSingletonFactory方法:

    protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
        Assert.notNull(singletonFactory, "Singleton factory must not be null");
        synchronized (this.singletonObjects) {
            if (!this.singletonObjects.containsKey(beanName)) {
                this.singletonFactories.put(beanName, singletonFactory);
                this.earlySingletonObjects.remove(beanName);
                this.registeredSingletons.add(beanName);
            }
        }
    }

把早期對(duì)象(早期對(duì)象是創(chuàng)建了但還沒初始化賦值的對(duì)象)放到緩存中氮墨,以解決循環(huán)依賴纺蛆。

繼續(xù)在doCreateBean往后看:

// 初始化bean實(shí)例.
Object exposedObject = bean;
try {
    populateBean(beanName, mbd, instanceWrapper);
    if (exposedObject != null) {
        exposedObject = initializeBean(beanName, exposedObject, mbd);
    }
}
catch (Throwable ex) {
    if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {
        throw (BeanCreationException) ex;
    }
    else {
        throw new BeanCreationException(
                mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);
    }
}

這里的核心的就是populateBean,對(duì)bean的屬性進(jìn)行賦值规揪,后面文章會(huì)單獨(dú)講桥氏。繼續(xù)看后面那個(gè)

initializeBean,是初始化bean猛铅,打開源碼看:

protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                invokeAwareMethods(beanName, bean);
                return null;
            }
        }, getAccessControlContext());
    }
    else {
        invokeAwareMethods(beanName, bean);
    }

    Object wrappedBean = bean;
    if (mbd == null || !mbd.isSynthetic()) {
        wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    }

    try {
        invokeInitMethods(beanName, wrappedBean, mbd);
    }
    catch (Throwable ex) {
        throw new BeanCreationException(
                (mbd != null ? mbd.getResourceDescription() : null),
                beanName, "Invocation of init method failed", ex);
    }

    if (mbd == null || !mbd.isSynthetic()) {
        wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    }
    return wrappedBean;
}

在這里是如果我們的Bean實(shí)現(xiàn)了BeanNameAware等aware的接口,在這里就會(huì)去來回調(diào)用我們的Bean接口實(shí)現(xiàn)類的方法字支。還調(diào)用了個(gè)invokeInitMethods也是這種開放式接口的用的,如果你的Bean實(shí)現(xiàn)了對(duì)應(yīng)的接口奕坟,在IOC這就會(huì)去調(diào)用你實(shí)現(xiàn)接口的方法代碼祥款。

最后是調(diào)用applyBeanPostProcessorsAfterInitialization,和AOP動(dòng)態(tài)有關(guān)的月杉,以后的文章講刃跛。

回到doCreateBean

現(xiàn)在只剩最后一段了:

//如果單例bean已經(jīng)緩存了,則直接獲取            
if (earlySingletonExposure) {
    Object earlySingletonReference = getSingleton(beanName, false);
    if (earlySingletonReference != null) {
        if (exposedObject == bean) {
            exposedObject = earlySingletonReference;
        }
//如果不允許在循環(huán)引用的情況下使用注入原始bean實(shí)例(即使注入的bean最終被包裝)苛萎,并且依賴的bean列表中存在需要?jiǎng)?chuàng)建bean桨昙。這時(shí)候就說明存在循環(huán)依賴
        else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
            //根據(jù)beanName獲取所有依賴的bean的beanName
            String[] dependentBeans = getDependentBeans(beanName);
            Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length);
            for (String dependentBean : dependentBeans) {
                //刪除存在循環(huán)依賴的bean    
                if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {
                    actualDependentBeans.add(dependentBean);
                }
            }
            if (!actualDependentBeans.isEmpty()) {
                throw new BeanCurrentlyInCreationException(beanName,
                        "Bean with name '" + beanName + "' has been injected into other beans [" +
                                StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +
                                "] in its raw version as part of a circular reference, but has eventually been " +
                                "wrapped. This means that said other beans do not use the final version of the " +
                                "bean. This is often the result of over-eager type matching - consider using " +
                                "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");
            }
        }
    }
}

// Register bean as disposable.
try {
    //將給定的bean添加到此一次性Bean列表中,這個(gè)列表中的bean在Spring關(guān)閉的時(shí)候會(huì)查詢里面的bean腌歉,并調(diào)用實(shí)現(xiàn)的銷毀方法(包含實(shí)現(xiàn)了DisposableBean接口的方法和自定義的destory方法)蛙酪,滿足其中一個(gè)條件
    // 1.實(shí)現(xiàn)了DisposableBean接口
    //2.自定義了destroy方法
    //3.實(shí)現(xiàn)了AutoCloseable接口
    registerDisposableBeanIfNecessary(beanName, bean, mbd);
}
catch (BeanDefinitionValidationException ex) {
    throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
}

這里就是把創(chuàng)建好的 bean實(shí)例加入 到我們的緩存 池

總結(jié)

最后總結(jié)一下過程

IOC的getbean第一次總結(jié).jpg
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市翘盖,隨后出現(xiàn)的幾起案子桂塞,更是在濱河造成了極大的恐慌,老刑警劉巖馍驯,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阁危,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡汰瘫,警方通過查閱死者的電腦和手機(jī)狂打,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來混弥,“玉大人趴乡,你說我怎么就攤上這事』饶茫” “怎么了晾捏?”我有些...
    開封第一講書人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)哀托。 經(jīng)常有香客問我粟瞬,道長(zhǎng),這世上最難降的妖魔是什么萤捆? 我笑而不...
    開封第一講書人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任裙品,我火速辦了婚禮,結(jié)果婚禮上俗或,老公的妹妹穿的比我還像新娘市怎。我一直安慰自己,他們只是感情好辛慰,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開白布区匠。 她就那樣靜靜地躺著,像睡著了一般帅腌。 火紅的嫁衣襯著肌膚如雪驰弄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,772評(píng)論 1 290
  • 那天速客,我揣著相機(jī)與錄音戚篙,去河邊找鬼。 笑死溺职,一個(gè)胖子當(dāng)著我的面吹牛岔擂,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播浪耘,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼乱灵,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了七冲?” 一聲冷哼從身側(cè)響起痛倚,我...
    開封第一講書人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎澜躺,沒想到半個(gè)月后蝉稳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡苗踪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年颠区,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片通铲。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡毕莱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出颅夺,到底是詐尸還是另有隱情朋截,我是刑警寧澤,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布吧黄,位于F島的核電站部服,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏拗慨。R本人自食惡果不足惜廓八,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一奉芦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧剧蹂,春花似錦声功、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至冒冬,卻和暖如春伸蚯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背简烤。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來泰國(guó)打工剂邮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人乐埠。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓抗斤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親丈咐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子瑞眼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348

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