AbstractApplicationContext的refresh()方法是spring的核心,在其中完成了容器的初始化
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();
}
}
}
1、prepareRefresh();
容器刷新前的准备,设置上下文状态,获取属性,验证必要的属性等
2、obtainFreshBeanFactory();
获取新的beanFactory,销毁原有beanFactory、为每个bean生成BeanDefinition
等
3、prepareBeanFactory(beanFactory);
配置标准的beanFactory,设置ClassLoader,设置SpEL表达式解析器,添加忽略注入的接口,添加bean,添加bean后置处理器等
4、postProcessBeanFactory(beanFactory);
模板方法,此时,所有的beanDefinition已经加载,但是还没有实例化。
允许在子类中对beanFactory进行扩展处理。比如添加ware相关接口自动装配设置,添加后置处理器等,是子类扩展prepareBeanFactory(beanFactory)的方法
5、invokeBeanFactoryPostProcessors(beanFactory);
实例化并调用所有注册的beanFactory后置处理器(实现接口BeanFactoryPostProcessor
的bean,在beanFactory标准初始化之后执行)。
例如:
PropertyPlaceholderConfigurer
(处理占位符)
6、registerBeanPostProcessors(beanFactory);
实例化和注册beanFactory中扩展了BeanPostProcessor
的bean。
例如:
AutowiredAnnotationBeanPostProcessor
(处理被@Autowired
注解修饰的bean并注入)
RequiredAnnotationBeanPostProcessor
(处理被@Required
注解修饰的方法)
CommonAnnotationBeanPostProcessor
(处理@PreDestroy
、@PostConstruct
、@Resource
等多个注解的作用)等。
7、initMessageSource();
初始化国际化工具类MessageSource
8、initApplicationEventMulticaster();
初始化事件广播器
9、onRefresh();
模板方法,在容器刷新的时候可以自定义逻辑,不同的Spring容器做不同的事情。
10、registerListeners();
注册监听器,广播early application events
11、finishBeanFactoryInitialization(beanFactory);
实例化所有剩余的(非懒加载)单例
比如invokeBeanFactoryPostProcessors方法中根据各种注解解析出来的类,在这个时候都会被初始化。
实例化的过程各种BeanPostProcessor开始起作用。
12、finishRefresh();
refresh做完之后需要做的其他事情。
清除上下文资源缓存(如扫描中的ASM元数据)
初始化上下文的生命周期处理器,并刷新(找出Spring容器中实现了Lifecycle
接口的bean并执行start()方法)。
发布ContextRefreshedEvent事件告知对应的ApplicationListener进行响应的操作