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

Spring之生命周期Bean的生成过程

Spring之生命周期Bean的生成过程

前言:一般面试题,
1、说说spring中Bean的生命周期?
2、我们可以在Bean的创建过程中,
3、怎么对Bean进行一些增强…
下面写的常见的Bean的生成过程,可能有误,欢迎大家一起指出

Bean的生命过程讲解

1,生成BeanDefinition

public class Test {

	public static void main(String[] args) {

		// 创建一个Spring容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
}

1.1 进到 AnnotationConfigApplicationContext 容器执行方法
org.springframework.context.support.AbstractApplicationContext#refresh

1.2 执行 完成Bean工厂的初始化
org.springframework.context.support.AbstractApplicationContext#finishBeanFactoryInitialization

1.3 执行 实例化非懒加载的单例Bean
org.springframework.beans.factory.config.ConfigurableListableBeanFactory#preInstantiateSingletons

获取Bean的定义
在这里插入图片描述
在这里插入图片描述

2,合并BeanDefinition

通过扫描得到BeanDefinition之后,就可以根据BeanDefinition创建Bean对象了,spring中支持BeanDefiniton的父子关系,比如

<bean id="parent" class="com.xx.service.Parent" scope="prototype"/>
<bean id="child" class="com.xxx.service.Child"/>

这么定义则 child 是单例
如果这么定义

<bean id="parent" class="com.xx.service.Parent" scope="prototype"/>
<bean id="child" class="com.xxx.service.Child" parent="parent"/>

则 child为prototype。
因为child的父BeanDefinition是parent,所以会继承父类的 scope属性

在这里插入图片描述

3,类加载器

执行 非抽象,单例 非懒加载 开始创建Bean
org.springframework.beans.factory.support.AbstractBeanFactory#getBean(java.lang.String)

org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean

开始调用
org.springframework.beans.factory.support.AbstractBeanFactory#resolveBeanClass

代码加载类
在这里插入图片描述
如果Bean没有被加载 则执行获取类加载器进行加载

在这里插入图片描述

	@Nullable
	private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();

1,优先选择当前线程的 ClassLoader
2,如果线程类加载器为null的情况下,则返回 ClassUtils.getDefaultClassLoader();的加载器
3,如果ClassUtils类的类加载器为空,则返回系统类的加载器执行的代码如下
ClassLoader.getSystemClassLoader();

4,实例化前

当BeanDefinition对应类加载成功后,就可以实现对象,在这里加个扩展点
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#resolveBeforeInstantiation

// 表示实例化之前做的,如果在这里进行实例化,会直接执后一步
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()
在这里插入图片描述

5,实例化

这里会根据BeanDefinition去创建一个对象。
在这里插入图片描述

6,BeanDefinition的后置处理器

Bean的对象实例化出来后,后面就是给属性赋值。在这spring提供一个扩展点
在这里插入图片描述

7,实例化后

spring提供一个扩展点实例化之后,属性设置之前 InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation()
在这里插入图片描述

7,自动注入

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

8,处理属性

这个步骤,会处理@Autowired、@Resource、@Value等注解
InstantiationAwareBeanPostProcessor.postProcessProperties()
在这里插入图片描述

9,执行Aware

完成了属性赋值之后,spring会执行一些回调包括如下
在这里插入图片描述

10,初始化前

BeanPostProcessor.postProcessBeforeInitialization()
常见的做拦截器用,token,权限…
实现类如下
在这里插入图片描述

11,初始化

判断是否实现InitializingBean 接口
在这里插入图片描述

12,初始化后

spring的扩展点
BeanPostProcessor.postProcessAfterInitialization()
【spring的Aop就是基于初始化之后实现的】

13,Bean的销毁

bean的销毁是在发送到spring容器关闭过程中的。

spring容器关闭时,比如

		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
		// 关闭容器
		applicationContext.close();

执行这个方法,销毁bean
org.springframework .beans.factory.support.AbstractBeanFactory#registerDisposableBeanIfNecessary
,
在这里插入图片描述
1,是否实现DisposableBean或者 AutoCloseable接口
2,BeanDefinition中是否指定了destroyMethod
3,调用DestructionAwareBeanPostProcessor.requiresDestruction(bean)进行判断
在这里插入图片描述

相关文章:

  • 对比 Vue2 选项式 API 与 Vue3 setup 语法
  • Ubuntu 22.04使用pigz多线程快速解压/压缩文件
  • 【MySQL基础-3】SQL语言详解:定义、分类、注意事项与注释
  • 【Python】06、流程控制语句
  • 系统开发资源
  • 大模型AI相关记录
  • sql靶场--布尔盲注(第八关)保姆级教程
  • OpenHarmony-分布式硬件关键技术
  • 2025-03-12 Python深度学习1——安装Anaconda与PyTorch库
  • 在 C# 中,is null 和 == null ‌不完全等价‌
  • 《算法篇:翻转字符串里的单词 - Java 多种解法详解》
  • IMX6ULL驱动开发Linux篇01
  • UFW 配置 Ubuntu 防火墙并设置防火墙规则
  • 基于MATLAB的冰块变化仿真
  • C#核心笔记——(五)框架概述
  • K8S中PV和PVC之间的关系
  • 大语言模型-1.2-大模型技术基础
  • css -学习
  • MySQL 数据归档自动化:Python + pt-archiver 打造高效运维利器
  • 【WebRTC---源码篇】(二十三_一)JitterBuffer运行流程
  • 乌美矿产协议文本公布,明确乌收益及协议优先级
  • 案件发回重审,李在明参选韩总统之路再添波折
  • 体坛联播|欧冠巴萨3比3战平国米,柯洁未进入国家集训队
  • 市场监管总局出手整治涉企乱收费,聚焦政府部门及下属单位等领域
  • 上海科创再出发:“造星”的城和“摘星”的人
  • 视频丨中国海警位中国黄岩岛领海及周边区域执法巡查