容器其他相关特性
ApplicationContext
ApplicationContext 自身有一个创建和销毁的生命周期
核心的内容全部在refresh中
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 环境设置
prepareRefresh();
// 刷新 beanFactory,这里包含了IOC容器的初始化
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 1。spel
// 2。属性处理
// 3。添加bean的后置处理器
// 4。忽略依赖
// 5。必要的依赖
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 子类重写,可以加入beanFactory的后处理器
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 调用后处理器
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 注册bean的后处理器,不会马上被调用,只有在bean被创建的时候
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.
// 初始化非懒加载的单例,默认将单例的bean马上进行初始化
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();
}
}
}