5

SpringBoot源码 - bean的加载(上)

 2 years ago
source link: https://segmentfault.com/a/1190000040773381
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SpringBoot源码 - bean的加载(上)

发布于 19 分钟前

我又来讲源码恶心大家了,嘿嘿~

上一节中讲的 run() 方法启动流程中,有那么一行代码:

refreshContext(context);

这一行代码就是今天的男主角了 - 它完成了bean的加载。它的实现在 AbstractApplicationContext 类的refresh()方法中,上码:

@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
    StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
    // 容器状态设置、初始化属性设置、验证必备属性
    prepareRefresh();
    // 获取beanFactory
    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    // 设置beanFactory的一些属性、添加后置处理器、注册默认的环境beans、
    prepareBeanFactory(beanFactory);
    try {
        // 空方法,给子类留个钩子注入 postProcessors
        postProcessBeanFactory(beanFactory);
        
        StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
        // 执行 BeanFactoryPostProcessor
        invokeBeanFactoryPostProcessors(beanFactory);

        // BeanPostProcessor注册进容器
        registerBeanPostProcessors(beanFactory);
        beanPostProcess.end();

        // 为容器初始化消息源
        initMessageSource();

        // 为容器注册事件广播器
        initApplicationEventMulticaster();

        // 空方法,留给子类注册其他bean
        onRefresh();

        // 注册事件监听器,派发之前未处理的事件
        registerListeners();

        // 初始化剩下的单实例bean
        finishBeanFactoryInitialization(beanFactory);

        // 初始化什么周期处理器,发布容器启动事件
        finishRefresh();
    }
        …………
        finally {
            // 清缓存
            resetCommonCaches();
            contextRefresh.end();
        }
    }
}

以上是spring容器启动的核心流程,下一节,重点来了~~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK