一.前言  

  在日常开发中,spring极大地简化了我们日常的开发工作。spring为我们管理好bean, 我们拿来就用。但是我们不应该只停留在使用层面,深究spring内部的原理,才能在使用时融汇贯通。

  这是spring原理系列的第一篇,本篇主要讲解spring容器中bean的生命周期。这是基础,先从全貌上了解spring bean的生命周期,有利于我们更好地深入理解bean的生命周期各阶段发生了什么。

二. spring bean生命周期

spring bean从出生到死亡经历如下历程:

(1)调用BeanFactoryPostProcessor的postProcessBeanFactory()方法

(2)实例化Bean

(3)Bean的属性设置

(4)调用Aware接口的方法

(5)调用BeanPostProcessor的postProcessorBeforeInitialization()方法

(6)Bean的初始化

(7) 调用BeanPostProcessor的postProcessorAfterInitialization()方法

(8)Bean的销毁

上面的方法分为两类:

(1)容器级别的方法

例如BeanFactoryPostProcessor和BeanPostProcessor

注意: BeanFactoryPostProcessor只会在容器启动时调用一次postProcessBeanFactory()方法,而BeanPostProcessor在每个bean初始化前后都会调用对应的方法。

(2)bean级别的方法

例如各种Aware接口的方法(常见的ApplicationContextAware), 初始化方法, bean销毁时的方法。

 

在AbstractApplicatonContext的refresh()方法中,可以清楚地看到spring容器刷新时的整个步骤,源代码如下:

public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh(); // Tell the subclass to refresh the internal bean 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.
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
registerBeanPostProcessors(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();
}
....

  后面的系列篇其实都是来解读上面这段代码的。

Spring原理系列一:Spring Bean的生命周期的更多相关文章

  1. Spring框架系列(8) - Spring IOC实现原理详解之Bean实例化(生命周期,循环依赖等)

    上文,我们看了IOC设计要点和设计结构:以及Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的:容器中存放的是Bean的定义即Be ...

  2. Spring学习-- IOC 容器中 bean 的生命周期

    Spring IOC 容器可以管理 bean 的生命周期 , Spring 允许在 bean 声明周期的特定点执行定制的任务. Spring IOC 容器对 bean 的生命周期进行管理的过程: 通过 ...

  3. Spring系列13:bean的生命周期

    本文内容 bean的完整的生命周期 生命周期回调接口 Aware接口详解 Spring Bean的生命周期 面试热题:请描述下Spring的生命周期? 4大生命周期 从源码角度来说,简单分为4大阶段: ...

  4. Spring重点—— IOC 容器中 Bean 的生命周期

    一.理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助. 二.在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理. 三.在没有添加后置处理器的情况 ...

  5. spring BeanFactory及ApplicationContext中Bean的生命周期

    spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...

  6. Spring 框架基础(02):Bean的生命周期,作用域,装配总结

    本文源码:GitHub·点这里 || GitEE·点这里 一.装配方式 Bean的概念:Spring框架管理的应用程序中,由Spring容器负责创建,装配,设置属性,进而管理整个生命周期的对象,称为B ...

  7. 【深入ASP.NET原理系列】--ASP.NET页面生命周期

    前言 ASP.NET页面运行时候,页面将经历一个生命周期,在生命周期中将执行一系列的处理步骤.包括初始化.实例化控件.还原和维护状态.运行时间处理程序代码以及进行呈现.熟悉页面生命周期非常重要,这样我 ...

  8. Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计

    在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...

  9. Spring框架系列(7) - Spring IOC实现原理详解之IOC初始化流程

    上文,我们看了IOC设计要点和设计结构:紧接着这篇,我们可以看下源码的实现了:Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的. ...

随机推荐

  1. 020、MySQL创建一个存储过程,显示存储过程,调用存储过程,删除存储过程

    一.我们创建一个MySQL储存过程,在SQL代码区写入以下内容,并执行就可以了 #编写一个存储过程 CREATE PROCEDURE ShowDate ( ) BEGIN #输出当前时间 SELECT ...

  2. 5.Linux解决Device eth0 does not seem to be present

    Linux操作系统排除故障 导入vixualbox的虚拟机voa文件到另外一台电脑,需要检查如下信息 修改虚拟机软件网络设置 重启Linux操作系统 shutdown -h now reboot se ...

  3. 【Jasypt】给你的配置加把锁

    前言 前几天,有个前同事向我吐槽,他们公司有个大神把公司的项目代码全部上传到了 github,并且是公开项目,所有人都可以浏览.更加恐怖的是项目里面包含配置文件,数据库信息.redis 配置.各种公钥 ...

  4. ASP.NETCore -----导入Excel文件

    前端上传excel文件利用npoi读取数据转换成datatable(netcore坑爹啊,用的vs2017竟然不能可视化) 前端界面 @{ Layout = null; } <!DOCTYPE ...

  5. List<string>绑定到DataGridView控件

    问题 将一个简单的List<string>作为数据源绑定到 DataGridView myDataGridView.DataSource = myStringList; 但是只得到一个名为 ...

  6. makecert 制作数字证书 给DLL加一个数字签名

    声明:文章整理自互联网 我仅需要给dll添加(替换)一个签名,所以我只看了第一步和第三步,其余的部分我没有测试,不能保证内容的是否正确. 看了很多关于DLL加签名的教程 大多是错误的 完全无法正常走下 ...

  7. 实验吧-杂项-pilot-logic、ROT-13变身了

    1.pilot-logic 题上说password藏在文件里,直接丢到Winhex里,搜索pass就拿到flag了. 有的大佬提供了另一种方法,题上说是一个磁盘文件,有一个处理磁盘文件的软件autop ...

  8. Sublime和Python中文编码的一些问题

    Windows下的控制台中,应该是这样的逻辑: 1.如果是Unicode字符串的话,首先根据控制台编码进行转换 2.之后进行输出 所以在Windows控制台下,假设str = u'中文', 1.直接p ...

  9. cf 498 B. Name That Tune

    不会不会,,,%%http://hzwer.com/5813.html #include<cstdio> #include<iostream> #include<algo ...

  10. Assignment写作谨慎学术抄袭是关键

    学术写作(Academic Writing)作为留学生涯的“必修课”,总是让闻者叹气,抓耳挠腮.初入课堂的留学生,更是缺乏写作经验不知从何下笔,只想仰天长啸“Essay真的好难啊!!”面对一个Essa ...