SpringIOC源码阅读—创建BeanFactroy—>Def

作者: 激情的狼王 | 来源:发表于2017-11-14 16:08 被阅读0次

上篇Spring-IOC源码---两大核心接口已经提到了两大基本的接口BeanFactory和BeanDefinition,简单概括就是

bean工厂 == BeanFactory的实现类
bean实现类 == BeanDefinition的实现类

bean工厂持有一个Map<String,BeanDefinition>,这样就可以在任何时候我们想用哪个bean,取到它的bean定义,我们就可以创造出一个新鲜的实例。

这次我们从源代码跟进去一步一步探索:

package xz.quartz.analysis;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class xz {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:beans.xml");
        System.out.println("beanNum: " + applicationContext.getBeanDefinitionCount());
        ((User)applicationContext.getBean("user")).go();
    }
}

这是通过spring获取到的一个User的bean实例,然后调用它的方法,
输出结果为:

beanNum: 1
I am going!

可以看出spring初始化的过程就是在main方法的第一行完成的,我们跟进去读一下FileSystemXmlApplicationContext的构造方法:

/**
     * Create a new FileSystemXmlApplicationContext, loading the definitions
     * from the given XML file and automatically refreshing the context.
     * @param configLocation file path
     * @throws BeansException if context creation failed
     */
    public FileSystemXmlApplicationContext(String configLocation) throws BeansException {
        this(new String[] {configLocation}, true, null);
    }

再跟进this(new String[] {configLocation}, true, null);

public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
            throws BeansException {

        super(parent);
        setConfigLocations(configLocations);
        if (refresh) {
            refresh();
        }
    }

这个方法里我们发现了三波干货

(1)

super(parent);
这行最终super到了顶层类AbstractApplicationContext里的setParent方法,作用是设置了上下文环境。

(2)

setConfigLocations(configLocations);这行跟进去

public void setConfigLocations(String... locations) {
        if (locations != null) {
            Assert.noNullElements(locations, "Config locations must not be null");
            this.configLocations = new String[locations.length];
            for (int i = 0; i < locations.length; i++) {
                this.configLocations[i] = resolvePath(locations[i]).trim();
            }
        }
        else {
            this.configLocations = null;
        }
    }

简单读一下setConfigLocations方法可以知道它将我们main方法传进来的classpath:beans.xml字符串参数设置到了this.configLocations里。也就是知道了bean的配置文件的位置,为初始化配置文件里的bean做准备。

到此上面两行基本理解了,现在只剩下最后一个了

(3)

if (refresh) {//其中refresh构造函数传进来默认为true
    refresh();
}

这个方法走完Spring的IOC的源码就结束了,是不是有些小激动?
继续跟进refresh()方法:

public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();

            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);

            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);

                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);

                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);

                // Initialize message source for this context.
                initMessageSource();

                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();

                // Initialize other special beans in specific context subclasses.
                onRefresh();

                // Check for listener beans and register them.
                registerListeners();

                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);

                // Last step: publish corresponding event.
                finishRefresh();
            }

            catch (BeansException ex) {
                if (logger.isWarnEnabled()) {
                    logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
                }

                // Destroy already created singletons to avoid dangling resources.
                destroyBeans();

                // Reset 'active' flag.
                cancelRefresh(ex);

                // Propagate exception to caller.
                throw ex;
            }

            finally {
                // Reset common introspection caches in Spring's core, since we
                // might not ever need metadata for singleton beans anymore...
                resetCommonCaches();
            }
        }
    }

呃...,你懂的,继续吧
第一行的话只是设置了容器的活动标志,true ,false等;
我们看第二行(这行是本文标题的核心处:创建BeanFactroy的过程就在这里了

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//有点像新建一个bean工厂的意思了

点进去obtainFreshBeanFactory()方法

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {
            logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
        }
        return beanFactory;
    }

第一行refreshBeanFactory()方法:

protected final void refreshBeanFactory() throws BeansException {
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }
        try {
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            beanFactory.setSerializationId(getId());
            customizeBeanFactory(beanFactory);
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }

这个方法加上了final关键字,也就是说此方法不可被重写,可以很清楚的看到,IOC容器的初始化就是在这个方法里发生的,第一步先是判断有无现有的工厂,有的话便会将其摧毁,然后就会通过createBeanFactory()创建一个默认的bean工厂。并且我们可以知道创建的bean工厂一定是上一篇提到的BeanFactory接口的实现(DefaultListableBeanFactory)。

然后我们继续看第二行

ConfigurableListableBeanFactory beanFactory = getBeanFactory();

第一行创建了工厂后,第二行就很明显了,直接getBeanFactory获取就可以。随后加锁synchronized赋值
备注:这个具体的工厂类DefaultListableBeanFactory存在于abstract类AbstractRefreshableApplicationContext中。

这就是spring初始化时BeanFactroy的创建过程。

下一篇我们继续聊创建完BeanFactroy后,BeanFactroy的Map<String,BeanDefinition>注册。

相关文章

网友评论

    本文标题:SpringIOC源码阅读—创建BeanFactroy—>Def

    本文链接:https://www.haomeiwen.com/subject/ckcfvxtx.html