refresh方法详解
最终调用的是AbstractApplicationContext.refresh
即对容器的刷新:
1. this.prepareRefresh()
主要做的事情:
设置容器状态
注意两点:
容器是容器,BeanFactory是BeanFactory,
在Springboot启动过程中,以web类型举例:
先创建容器:context = this.createApplicationContext()
创建出的是AnnotationConfigServletWebServerApplicationContext类型的容器,return new AnnotationConfigServletWebServerApplicationContext()
然后执行三步:
this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
重点:
(1)创建beanFactory,整个过程的主角是ConfigurableListableBeanFactory
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
(2)beanFactory预处理
this.prepareBeanFactory(beanFactory);
this.postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
this.initMessageSource();
this.initApplicationEventMulticaster();
this.onRefresh();
重点
this.registerListeners();
this.finishBeanFactoryInitialization(beanFactory);
重点:开启bean的加载过程
this.finishRefresh();
this.afterRefresh(context, applicationArguments);