软件编程
位置:首页>> 软件编程>> java编程>> spring Bean创建的完整过程记录

spring Bean创建的完整过程记录

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

标签:spring,bean,实例

前言

复习一下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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com