对于普通的Java对象,当new的时候创建对象,当它没有任何引用的时候被垃圾回收机制回收。而由Spring IoC容器托管的对象,它们的生命周期完全由容器控制。

Spring IoC容器托管对象的bean声明有以下四种方式

加载解析配置信息后会封装成一个BeanDefinition对象(bean的定义信息),然后会根据bean的信息进行初始化,然后实例化,然后被使用,最后被销毁。

核心代码
org.springframework.context.support.AbstractApplicationContext

 @Overridepublic void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.准备工作包括设置启动时间,是否激活标识位,// 初始化属性源(property source)配置prepareRefresh();// Tell the subclass to refresh the internal bean factory.//返回一个factory 为什么需要返回一个工厂//因为要对工厂进行初始化ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();// Prepare the bean factory for use in this context.//准备工厂prepareBeanFactory(beanFactory);try {// Allows post-processing of the bean factory in context subclasses.//这个方法在当前版本的spring是没用任何代码的//可能spring期待在后面的版本中去扩展吧postProcessBeanFactory(beanFactory);// Invoke factory processors registered as beans in the context.//在spring的环境中去执行已经被注册的 factory processors//设置执行自定义的ProcessBeanFactory 和spring内部自己定义的invokeBeanFactoryPostProcessors(beanFactory);// Register bean processors that intercept bean creation.//注册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.registerListeners();// Instantiate all remaining (non-lazy-init) singletons.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();}}}

Spring中的bean的生命周期主要包含四个阶段:实例化Bean --> Bean属性填充 --> 初始化Bean -->销毁Bean

有关Spring Bean生命周期最主要的方法有三个invokeBeanFactoryPostProcessorsregisterBeanPostProcessorsfinishBeanFactoryInitialization

以下论述内容依据版本是 spring-boot 2.2.8-release
在类AbstractApplicationContext中,有关键成员变量:List beanFactoryPostProcessors; 是一组bean 工厂后置处理器。

invokeBeanFactoryPostProcessors方法: 会执行BeanFactoryPostProcessors后置处理器及其子接口BeanDefinitionRegistryPostProcessor,执行顺序先是执行BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry方法,然后执行BeanFactoryPostProcessor接口的postProcessBeanFactory方法。

BeanDefinitionRegistryPostProcessor接口的postProcessBeanDefinitionRegistry方法:该步骤会扫描到指定包下面的标有注解的类,然后将其变成BeanDefinition对象,然后放到一个Spring中的Map中,用于下面创建Spring bean的时候使用这个BeanDefinition 由于spring-boot中bean 生产(暂存)工厂是:DefaultListableBeanFactory, 上述过程可以参考DefaultListableBeanFactory的registerBeanDefinition方法。

registerBeanPostProcessors方法:根据实现了PriorityOrdered、Ordered接口,排序后注册所有的BeanPostProcessor后置处理器,主要用于Spring Bean创建时,执行这些后置处理器的方法,这也是Spring中提供的扩展点,让我们能够插手Spring bean创建过程。

springboot bean生命周期相关推荐

  1. SpringBoot:Bean生命周期介绍

    SpringBoot:Bean生命周期介绍 前言 一.Bean 的生命周期介绍 二.具体操作顺序 前言 Bean 是 SpringBoot 中一个非常重要的组成部分,是面试中经常会问到的一个问题. 本 ...

  2. springboot学习:bean生命周期

    1.bean 生命周期 bean创建-初始化-销毁 构造(对象创建): 单实例:在容器启动的时候创建对象; 多实例:在每次获取的时候创建对象: 初始化: 对象创建完成,并赋值好,调用初始化方法 销毁: ...

  3. Spring Bean生命周期: Bean的实例化

    [Spring Bean 生命周期系列]传送门 1.Spring Bean生命周期: Bean元信息的配置与解析阶段 2.Spring Bean生命周期: Bean的注册 3.Spring Bean生 ...

  4. Spring Bean生命周期:Bean的初始化阶段

    [Spring Bean 生命周期系列]传送门 1.Spring Bean生命周期: Bean元信息的配置与解析阶段 2.Spring Bean生命周期: Bean的注册 3.Spring Bean生 ...

  5. spring一: 容器以及bean生命周期

    spring框架的几个优点: 1. 轻量级  2. 针对接口编程,解耦合   3. aop编程   4. 方便集成各种优秀框架  5.非侵入式   6.容器 IOC(Inversion of Cont ...

  6. 【Spring】- Bean生命周期

    2019独角兽企业重金招聘Python工程师标准>>> Spring Bean的生命周期: bean对象实例化 属性注入 beanfactory ApplicationContext ...

  7. Spring5源码 - 07 Spring Bean 生命周期流程 源码解读02

    文章目录 Pre 通俗流程 finishBeanFactoryInitialization Pre Spring5源码 - 06 Spring Bean 生命周期流程 概述 01 接上文 通俗流程 下 ...

  8. Spring5源码 - 06 Spring Bean 生命周期流程 概述 01

    文章目录 Bean生命周期概述 Demo finishBeanFactoryInitialization(beanFactory) 核心流程 Bean生命周期概述 说到Spring Bean的生命周期 ...

  9. Spring学习笔记八--Bean生命周期和后置处理器

    为什么80%的码农都做不了架构师?>>>    Bean生命周期和后置处理器 IOC容器的bean生命周期 1.构造器或工厂方法建立bean实例 2.bean属性赋值,引用其他bea ...

最新文章

  1. UML类图、接口、包、关系
  2. Python--32 模块 包
  3. ICC_lab总结——ICC_lab4:时钟树综合
  4. php上传文件 不移动,move_uploaded_file()为什么无法移动上传的文件?
  5. 解决Jenkins打不开浏览器问题
  6. mysql 触发器和存储过程组合使用,实现定时触发操作
  7. 读《scikiit-learn机器学习》黄永昌第三章
  8. random随机数类
  9. nginx反向代理,调度器,优化
  10. OSEK NM 功能测试
  11. 向量点乘(即内积)和叉乘(即外积、向量积)区别与意义分析
  12. 65个最常见的面试问题与技巧性答复(面试技巧和注意事项),很不错,求职之前,多看看
  13. 中职计算机应用基础word表格,计算机应用基础-word表格编辑.ppt
  14. 在 HBuilder X 创建Uni-app项目运行时报错
  15. 机器学习样本数据集,训练正负样本
  16. 【Parsec + ZeroTier】校园网内免费远程桌面(支持游戏)
  17. Nat. Commun. | 可多层次预测多肽-蛋白质相互作用的深度学习框架
  18. 吃鸡ios和android灵敏度,和平精英灵敏度怎么调最稳2020二指攻略:安卓苹果灵敏度调节方法大全[多图]...
  19. wk算法-SAR成像算法系列(五)
  20. 01654报错: 索引无法通过8扩展

热门文章

  1. Ericsson移动网络研讨会报告
  2. xshell传文件到虚拟机linux,VM虚拟机和主机互传文件,使用xshell连接Ubuntu
  3. 嵌入式 RTOS多任务操作系统简介
  4. LeetCode--fifty-four
  5. Paxos的实际应用
  6. php 指纹登录,window_win10系统中怎么设置指纹登陆?,现在很多电脑已经直接升级到 - phpStudy...
  7. 防止表单重复提交的方法
  8. Unity-协程详解
  9. 搜狗SEO优化技巧,搜狗收录批量查询技巧
  10. 各大搜索引擎网站收录入口