当前位置: 首页 > 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没有被加载 则执行获取类加载器进行加载

在这里插入图片描述

	@Nullableprivate 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)进行判断
在这里插入图片描述


http://www.mrgr.cn/news/94195.html

相关文章:

  • 看 MySQL InnoDB 和 BoltDB 如何写磁盘
  • uni-app打包成H5使用相对路径
  • C# Enumerable类 之 集合操作
  • c#面试题整理9
  • linux - ubuntu 使用时一些小问题整理 --- 持续更新
  • 数据结构——单链表list
  • 关于sqlalchemy的ORM的使用
  • Spring Security的作用
  • 浏览器控制台的特殊执行方式,暂存变量的作用域 和 同步/异步行为解释
  • 13 | 实现统一的错误返回
  • C# 通过chrome插件将HTML网页转换为PDF
  • JVM内存结构笔记03-方法区
  • 18 | 实现简洁架构的 Handler 层
  • 系统开发资源
  • 51c自动驾驶~合集54
  • IEC61850标准下MMS 缓存报告控制块 ResvTms详细解析
  • JVM内存结构笔记04-字符串常量池
  • 15 | 定义简洁架构 Store 层的数据类型
  • CMD批处理一些冷门命令,编写windows脚本常用?
  • 10 | 基于 Gin 实现 HTTP 服务器