Spring IOC
- 基于xml配置实例化工厂 ,实例化 ClassPathXmlApplicationContext 或者 FileSystemXmlApplicationContext效果一样,最终执行为AbstractApplicationContext的 refresh() 方法。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
prepareRefresh();
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
prepareBeanFactory(beanFactory);
try {
postProcessBeanFactory(beanFactory);
invokeBeanFactoryPostProcessors(beanFactory);
registerBeanPostProcessors(beanFactory);
initMessageSource();
initApplicationEventMulticaster();
onRefresh();
registerListeners();
finishBeanFactoryInitialization(beanFactory);
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
destroyBeans();
cancelRefresh(ex);
throw ex;
}
finally {
resetCommonCaches();
}
}
}
- 我们先大致说下该方法的步骤
- 构建BeanFactory,以便于产生所需要的Bean
- 注册可能感兴趣的事件
- 常见Bean实例对象
- 触发被监听的事件