Spring版本: 5.1.8.RELEASE
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
因为我们实例采用注解驱动的,因此我们的容器实例为:AnnotationConfigApplicationContext
分析类的继承关系
refresh()方法是容器启动最重要的流程,没有之一,里面完成了容器的初始化、Bean的初始化、依赖注入的实现、AOP的实现等重要过程都在此方法中实现。
下边我们将开始分析refresh()方法过程:
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
this(); //初始化过程详见 ;// AnnotationConfigApplicationContext初始化过程
register(annotatedClasses); // AnnotationConfigApplicationContext初始化过程
refresh();
}
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
//1、容器刷新前的准备。作用:设置上下文状态,获取属性,验证必要的属性、初始化propertySources由子类进行实现、初始化监听器事件、对Listener进行合并处理
// 详见: Spring启动流程核心步骤 :1、prepareRefresh()
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
//2、获取beanFactory容器。默认采用的是DefaultListableBeanFactory
//详见: Spring启动流程核心步骤:2、obtainFreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 3、 初始化classLoader、初始化两个Bean处理器、初始化环境相关的Bean等
// 详见 Spring启动流程核心步骤:3、prepareBeanFactory(beanFactory);
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
//4、对于不同子类做不同的事情,给子类一个搞事情的机会。
// 详见:Spring启动流程核心步骤:4、postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
//5、调用所有注册的beanFactory后置处理器(实现接口BeanFactoryPostProcessor的bean)。
//详见:Spring启动流程核心步骤:5、invokeBeanFactoryPostProcessors(beanFactory);
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
//6、注册Bean的处理器,在Bean的创建过程当中起作用。
//详见:Spring启动流程核心步骤:6、registerBeanPostProcessors(beanFactory);
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
//7、初始化国际化相关的内容
//详见: Spring启动流程核心步骤:7、initMessageSource();
initMessageSource();
// Initialize event multicaster for this context.
// 初始化事件发射器
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
//给子类一个机会来初始化一些特殊的Bean、做一些其他的事情
onRefresh();
// Check for listener beans and register them.
//8、注册监听器,把容器中实现监听接口的Bean也保存起来。并且广播early application events,也就是早期的事件
//详见 :Spring启动流程核心步骤:8、registerListeners();
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
//9、初始化Bean。
//详见:Spring启动流程核心步骤:9、finishBeanFactoryInitialization(beanFactory);
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
//清除上下文资源缓存(如扫描中的ASM元数据)
//初始化上下文的生命周期处理器,并刷新(找出Spring容器中实现了Lifecycle接口的bean并执行start()方法)。
//发布ContextRefreshedEvent事件告知对应的ApplicationListener进行响应的操作
//详见 :Spring启动流程核心步骤:10、finishRefresh();
finishRefresh();
}catch (BeansException ex) {
//失败就会销毁Bean抛出异常
throw ex;
}finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
参考资料:https://blog.youkuaiyun.com/f641385712/article/details/88041409