spring容器IOC启动流程

spring容器IOC初始化流程

启动核心类:AbstractApplicationContext
启动方法:refresh()
特点:synchronized,保证唯一线程

源码解析
 public void refresh() throws BeansException, IllegalStateException {
        synchronized(this.startupShutdownMonitor) {
            //1.刷新预处理
            this.prepareRefresh();
            
            //2.获取 BeanFactory
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            //3.BeanFactory准备工作
            this.prepareBeanFactory(beanFactory);

            try {
               // 4.BeanFactory准备工作完成后进行的后置处理工作;
                this.postProcessBeanFactory(beanFactory);
                
                //5.执行BeanFactoryPostProcessor的方法
                this.invokeBeanFactoryPostProcessors(beanFactory);
                
                //6.注册BeanPostProcessor
                this.registerBeanPostProcessors(beanFactory);
                
                //7.初始化MessageSource组件(做国际化功能;消息绑定,消息解析)
                this.initMessageSource();
                
                //8.初始化事件派发器;
                this.initApplicationEventMulticaster();
                
                //9.留给子容器(子类)
                this.onRefresh();
                
                //10.给容器中将所有项目里面的ApplicationListener注册进来;
                this.registerListeners();
                
                //11.初始化所有剩下的单实例bean;
                this.finishBeanFactoryInitialization(beanFactory);
                
               //12.完成BeanFactory的初始化创建工作;IOC容器就创建完成;
                this.finishRefresh();
            } catch (BeansException var9) {
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
                }
                //销毁beans
                this.destroyBeans();
                this.cancelRefresh(var9);
                throw var9;
            } finally {
            //清理缓存
                this.resetCommonCaches();
            }

        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容