1 .AbstractApplicationContext prepareBeanFactory
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
//刷新前预处理
prepareRefresh();
//获取beanFactory实例。(DefaultListableBeanFactory)
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
//beanFactory属性赋值
prepareBeanFactory(beanFactory);
try {
// 子类通过重写这个方法在BeanFactory创建并预准备完成以后做进一步设置
postProcessBeanFactory(beanFactory);
// 调用实现BeanDefinitionRegistryPostProcessor,BeanFactoryPostProcessor 接口的类相关方法
invokeBeanFactoryPostProcessors(beanFactory);
// 注册实现了BeanPostProcessor接口的Bean
registerBeanPostProcessors(beanFactory);
// 标签国际化资源,初始化MessageSource组件
initMessageSource();
// 创建事件广播器
initApplicationEventMulticaster();
// 空方法,留给子容器实现,在容器刷新的时候可以子定义逻辑
onRefresh();
// 注册监听器事件
registerListeners();
// 单例模式bean的注册
finishBeanFactoryInitialization(beanFactory);
// 刷新完成容器
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();
}
}
}
2 核心方法解析
2.1 obtainFreshBeanFactory() 获取BeanFactory
(1) AbstractApplicationContext obtainFreshBeanFactory
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
//刷新容器
refreshBeanFactory();
//获取GenericApplicationContext创建的容器
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
GenericApplicationContext refreshBeanFactory()
2.2 invokeBeanFactoryPostProcessors(beanFactory)
实际调用PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors
(1) 调用实现BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry,postProcessBeanFactory方法
(2) 调用实现类BeanFactoryPostProcessor接口的postProcessBeanFactory方法
PS:BeanFactoryPostProcessor源码机票 -------> https://blog.csdn.net/qq_34125999/article/details/104645988
2.3 registerBeanPostProcessors(beanFactory);
实际调用PostProcessorRegistrationDelegate registerBeanPostProcessors,其作用就是给工厂中添加实现了BeanPostProcessor接口的bean
PS:源码机票 -------> https://blog.csdn.net/qq_34125999/article/details/104572875
2.4 finishBeanFactoryInitialization(beanFactory);
完成单实例Bean的注册。
PS:源码机票 -------> https://blog.csdn.net/qq_34125999/article/details/104547157
3 单例Bean注册的位置
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15152.html