当前位置: 首页 > news >正文

Mr. Cappuccino的第63杯咖啡——Spring之AnnotationConfigApplicationContext源码分析

Spring之AnnotationConfigApplicationContext源码分析

    • 源码分析

源码分析

以上一篇文章《Spring之Bean的生命周期》的代码进行源码分析

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig02.class);
LifeCycleBean lifeCycleBean = applicationContext.getBean("lifeCycleBean", LifeCycleBean.class);
System.out.println("-----------------------------------");
System.out.println("LifeCycleBean " + lifeCycleBean);
System.out.println("-----------------------------------");
applicationContext.close();

new AnnotationConfigApplicationContext(SpringConfig02.class)

AnnotationConfigApplicationContext.java

在这里插入图片描述

AbstractApplicationContext.java

@Override
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.// 容器刷新前的准备工作,例如对系统属性、环境变量进行准备及验证prepareRefresh();// Tell the subclass to refresh the internal bean factory.// 初始化BeanFactoryConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.// 对BeanFactory进行属性填充及功能填充,例如添加某些BeanPostProcessor以实现某些功能(ApplicationContextAwareProcessor)prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.// 空方法,用于子类实现扩展功能postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.// 执行BeanFactoryPostProcessor,例如ConfigurationClassPostProcessor、EventListenerMethodProcessor以及某些自定义的BeanFactoryPostProcessor// ConfigurationClassPostProcessor用于处理配置类,会将配置类中需要注册的Bean对象的元数据解析并存入beanDefinitionMap集合中invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.// 注册BeanPostProcessor,例如AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及某些自定义的BeanPostProcessorregisterBeanPostProcessors(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.// 注册所有的ApplicationListener到消息多播器registerListeners();// Instantiate all remaining (non-lazy-init) singletons.// 实例化、初始化剩下的非懒加载的单例对象finishBeanFactoryInitialization(beanFactory);// Last step: publish corresponding event.// 完成刷新动作,启动LifecycleProcessor,发布ContextRefreshedEvent事件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();}}
}

BeanFactory中两个重要的对象

DefaultListableBeanFactory.java

/** Map of bean definition objects, keyed by bean name. */
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(256);

DefaultSingletonBeanRegistry.java

/** Cache of singleton objects: bean name to bean instance. */
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

Debug源码分析

在这里插入图片描述
在这里插入图片描述

register(componentClasses):注册指定的配置类SpringConfig02到beanDefinitionMap集合中

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

invokeBeanFactoryPostProcessors(beanFactory):创建BeanFactoryPostProcessor接口相关实现类的实例对象并存入singletonObjects集合中;执行BeanFactoryPostProcessor接口相关实现类的方法,通过ConfigurationClassPostProcessor处理配置类中需要注册的bean对象,并将bean对象注册到beanDefinitionMap集合中。

在这里插入图片描述

registerBeanPostProcessors(beanFactory):注册BeanPostProcessor接口相关实现类,创建BeanPostProcessor接口相关实现类的实例对象并存入singletonObjects集合中

在这里插入图片描述
在这里插入图片描述

finishBeanFactoryInitialization(beanFactory):实例化、初始化剩下的非懒加载的单例对象并存入singletonObjects集合中

在这里插入图片描述

beanFactory.preInstantiateSingletons():提前加载单例对象


http://www.lryc.cn/news/130665.html

相关文章:

  • opencv直方图与模板匹配
  • Apache Doris 入门教程31:计算节点
  • Nacos和GateWay路由转发NotFoundException: 503 SERVICE_UNAVAILABLE “Unable to find
  • 2021年9月全国计算机等级考试真题(二级C语言)
  • 串口通讯
  • 自动拉取 GitHub 仓库更新的脚本
  • 如何获得Android 14复活节彩蛋
  • 国产32位单片机XL32F001,带1 路 12bit ADC,I2C、SPI、USART 等外设
  • typescript基础之null和undefined
  • php_mb_strlen指定扩展
  • 利用OpenCV光流算法实现视频特征点跟踪
  • 探索无限创造力的星辰大道,画出想象的浩瀚宇宙!-turtle
  • 企业数字化转型大数据湖一体化平台项目建设方案PPT
  • 【3Ds Max】车削命令的简单使用(以制作花瓶为例)
  • Python 3 使用HBase 总结
  • Maven方式构建SpringBoot项目
  • 不花一分钱,利用免费电脑软件将视频MV变成歌曲音频MP3
  • 运营知识之用户运营(一)触达用户的几种方式
  • cocos creator pageView 循环展示 广告牌功能
  • PyTorch Lightning:通过分布式训练扩展深度学习工作流
  • 无涯教程-Perl - splice函数
  • 归并排序:从二路到多路
  • 【Vue】运行项目报错 This dependency was not found
  • Shell编程之正则表达式
  • QGraphicsView 实例3地图浏览器
  • Windows基础安全知识
  • 自定义注解和自定义注解处理器来扫描所有带有某个特定注解的Controller层
  • 浏览器渲染原理 - 输入url 回车后发生了什么
  • 大文本的全文检索方案附件索引
  • 35_windows环境debug Nginx 源码-CLion配置CMake和启动