
// 启动方法
// 启动完,所有的单例都要实例化,对于已经创建的单例对象先销毁,在创建
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
// 1.启动前的准备工组,准备一些启动开始时间,激活标志、开关标识等。
this.prepareRefresh();
// 2.创建bean工厂,即ioc容器,已经有的销毁重新创建
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
// 3.初始化bean工厂,初始化标准工厂需要的属性,例如:类加载器、后置处理器等
this.prepareBeanFactory(beanFactory);
try {
// 4. 修改bean工厂的后置处理器,所有bean 的定义(即将bean对象加载为BeanDefination)的都会加载。
this.postProcessBeanFactory(beanFactory);
// 5.获取所有的bean工厂后置处理器,将其注册到ioc容器中。
this.invokeBeanFactoryPostProcessors(beanFactory);
// 6.实例化并注册bean的后置处理器,用来拦截bean的创建,可以再次进行自定义扩展,对bean的创建做定制化 *** 作
this.registerBeanPostProcessors(beanFactory);
// 7.初始化消息资源,例如:项目需要支持多国语言;可以实现容器级的国际化信息资源。不需要硬编码。
this.initMessageSource();
// 8.初始化事件
this.initApplicationEventMulticaster();
// 9.初始化 应用上下文子类特有的bean(默认是空的)。,
this.onRefresh();
// 10.注册监听器,将所有实现了ApplicationListener接口的监听器注册到ioc容器中。
this.registerListeners();
// 11.实例化剩余的所有单例对象(非懒加载的)
// 我们自己写的bean需要加载到spring容器中在此步骤进行
this.finishBeanFactoryInitialization(beanFactory);
// 12.完成应用上下文的启动刷新。清除环境级别的缓存资源,调用spring生命周期nRefresh()方法和发布事件。
this.finishRefresh();
} catch (BeansException var9) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
}
this.destroyBeans();
this.cancelRefresh(var9);
throw var9;
} finally {
this.resetCommonCaches();
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)