spring Bean创建的完整过程记录

作者:Jimmyhe 时间:2022-04-14 03:57:51 

前言

复习一下spring实现IOC的源码流程准备工作:

强烈建议大家从git上拉取spring源码来学习Spring源码。因为里面相较于IDEA生成的会有注释,里面有的方法会有注释看起来会省力一点。

以下都是用5.0.2版本来做阐述。

bean创建的流程图

写在前面:建议大家一定要自己用实例跑一遍,做好记录。如果只是看看会非常抽象。此流程图作为梗概,便于加强记忆和理解,新手或无基础的有个印象即可。等跟随本文走通一遍,在回过头看这个图,或许会有收获

spring Bean创建的完整过程记录

源码走一遍bean的定义这是我的bean目录结构,只是做一个例子

spring Bean创建的完整过程记录

获取核心容器对象,bean最后都会放在此容器对象中

*   ApplicationContext的三个实现类
   *   ClassPathXmlApplicationContext  它可以加载类路径下的配置文件,要求必须在类路径下
   *   FileSystemXmlApplicationContext  可以加载任意路径下的配置文件,必须有访问权限
   *   AnnotationConfigApplicationContext 用于读取注解创建容器的

这里我用ClassPathXmlApplicationContext来做演示

public class MyTest {
   public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
   }
}

快速开始

建议用IDEA的debug模式来观察Spring的IOC过程

spring Bean创建的完整过程记录

进入到此类的构造方法中

spring Bean创建的完整过程记录

查看setConfigLocations,就是将配置文件加载到configLocations里去

spring Bean创建的完整过程记录

向下执行,查看refresh()

spring Bean创建的完整过程记录

this.prepareRefresh(): 此方法是准备工作,大家感兴趣可以点进去看一下,可以看到里面是获取时间,获取环境信息的一些设置。

this.obtainFreshBeanFactory(): 这一步是创建beanFactory,并且读取Bean的信息,源码注释中还有写到

// Tell the subclass to refresh the internal bean factory.会告诉子类去刷新内部bean工厂

this.refreshBeanFactory:

* This implementation performs an actual refresh of this context's underlying
* bean factory, shutting down the previous bean factory (if any) and
* initializing a fresh bean factory for the next phase of the context's lifecycle.

这个实现类的方法会刷新容器中的beanFactory,关闭之前存在的并且初始化新的beanFactory

spring Bean创建的完整过程记录

利用this.createBeanFactory() 创建了一个beanFactory,类型为DefaultListableBeanFactory

这个类接着往下走:this.loadBeanDefinitions(beanFactory);

* Load bean definitions into the given bean factory, typically through
* delegating to one or more bean definition readers.

这个方法会将beandefinitionsReader读取到的bean definitions放入bean工厂,我们以上提出的三种
注入方式都会走到这里,将bean信息丢进去

返回上述 refresh()

this.prepareBeanFactory(beanFactory); 设置和忽略一些对象值

this.postProcessBeanFactory(beanFactory); 空方法可自定义扩展

this.invokeBeanFactoryPostProcessors(beanFactory);

* Instantiate and invoke all registered BeanFactoryPostProcessor beans,
* respecting explicit order if given.
* <p>Must be called before singleton instantiation.

实例化所有beanFactory组件

registerBeanPostProcessors(beanFactory);

Instantiate and register all BeanPostProcessor beans,  //先注册再调用

initApplicationEventMulticaster(); 观察者模式 * , 监听组件的相关状态,并决定相关调用方法。

finishBeanFactoryInitialization(beanFactory); 重要!!

* Finish the initialization of this context's bean factory,
* initializing all remaining singleton beans.

完成了容器bean factory的初始化,并且初始化其他的bean单例对象

beanFactory.preInstantiateSingletons(); 实例化方法

此方法最后this.getBean(beanName)

继续

spring Bean创建的完整过程记录

Return an instance, which may be shared or independent, of the specified bean.

注释已经很清楚了,此方法会返回一个实例,就是我们的bean对象

spring Bean创建的完整过程记录

进入到createBean方法中

spring Bean创建的完整过程记录

继续进入![image-20200714221630608](/Users/hjj/Library/Application Support/typora-user-images/image-20200714221630608.png)

继续进入

spring Bean创建的完整过程记录

spring Bean创建的完整过程记录

Instantiate the given bean using its default constructor.

这个方法注释说明了实例化对象是用构造器完成的

继续看他如何构造的

spring Bean创建的完整过程记录

spring Bean创建的完整过程记录

spring Bean创建的完整过程记录

ca 就是Constructor,从这里我们基本可以看出容器内,bean对象的实例化
是利用反射的基本原理,获取类构造器,然后newInstance来实现的

来源:https://www.cnblogs.com/jimmyhe/p/13976202.html

标签:spring,bean,实例
0
投稿

猜你喜欢

  • spring boot如何使用POI读取Excel文件

    2022-09-19 21:26:33
  • JAVA基本概念详解

    2022-06-09 11:54:53
  • 详解Spring MVC CORS 跨域

    2023-11-25 08:04:37
  • Java多线程编程中ThreadLocal类的用法及深入

    2022-03-17 03:21:29
  • 使用Postman传递arraylist数据给springboot方式

    2022-08-27 01:13:01
  • Mybatis generator自动生成代码插件实例解析

    2022-06-04 22:52:33
  • Spring中的aware接口详情

    2023-11-29 10:48:29
  • Java序列化JSON丢失精度问题的解决方法(修复Long类型太长)

    2022-10-15 00:01:34
  • Spring Cloud中Sentinel的两种限流模式介绍

    2021-11-22 00:38:10
  • springboot集成mybatisplus的方法

    2022-08-02 16:35:12
  • 基于Java接口回调详解

    2023-11-09 00:03:11
  • spring Boot打包部署到远程服务器的tomcat中

    2023-01-14 21:45:28
  • Java实现抢红包功能

    2021-08-05 07:47:58
  • 详解Java数据库连接JDBC基础知识(操作数据库:增删改查)

    2023-08-22 23:47:37
  • Java Synchronized锁失败案例及解决方案

    2023-10-25 12:33:47
  • python只需30行代码就能记录键盘的一举一动

    2023-06-27 18:22:12
  • SpringCloud Feign转发请求头(防止session失效)的解决方案

    2022-08-29 12:25:59
  • OpenCV和C++实现图像的翻转(镜像)、平移、旋转、仿射与透视变换

    2023-07-14 23:47:22
  • Spring MVC请求参数接收的全面总结教程

    2023-11-28 19:44:47
  • Java中遍历Map的六种方法实现

    2022-03-21 13:30:58
  • asp之家 软件编程 m.aspxhome.com