一.前言  

  在日常开发中,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. Apache http 包中的常量

    org.apache.* org.apache.http.Consts public static final int CR 13 public static final int HT 9 publi ...

  2. C++远征--jame_yuan(慕课网)

    int &a=b;引用符 int const a = 3; int *p = &a;     只读 不能 赋给 读写 有默认参数值得参数必须在参数表的最右端 void fun(int ...

  3. 在xampp下安装thinkphp5

    在xampp2016下安装thinkphp5 (本人的坑,说白了就是把thinkphp5的文件放到htdocs下) 1.把该xampp中的php文件夹路径设置进环境变量,这样才能全局运行compose ...

  4. swarm docker-compose.yml NFS 搭建LNMP

    列表: 172.16.0.40     (swarm-master) 172.16.0.160     (swarm-worker) 172.16.0.170     (swarm-worker) 1 ...

  5. SQLmap自动注入工具命令(10.28 10.29 第二十八 二十九天)

    SQL注入工具:明小子  啊D   萝卜头   sqlmap  等等 SQLMAP:开源的自动化诸如利用工具,支持的数据库有12中,在/plugins中可以看到支持的数据库种类,在所有注入利用工具中他 ...

  6. 每天一点点之 uni-app 框架开发 - 页面滚动到指定位置

    项目需求:在页面中,不管位于何处,点击评论按钮页面滚动到对应到位置 实现思路如下: uni.createSelectorQuery().select(".comment").bou ...

  7. POJ 3614:Sunscreen 贪心+优先队列

    Sunscreen Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5378   Accepted: 1864 Descrip ...

  8. 批量处理文件的Python程序

    经常批量处理文件,这里有个python的模板,保存一下 这个例子是把目录里面所有子目录的mp3文件放慢0.85倍并保存到./processed/目录下面. #coding=utf-8 import s ...

  9. node - TypeError: argument handler must be a function 路由报错的原因

    原因 : 路由一定要暴露给外部使用   var express = require('express') var router = express.Router()   router.get('/', ...

  10. [YOLO]《YOLO9000:Better, Faster, Stronger》笔记

    一.简单介绍 YOLO9000(也叫YOLO v2),主要是在YOLO v1的基础上做了改进,而且效果还是比较显著的,在原论文中,作者提到的改进大致包括两个工作: 1.检测性能上的改进,提出了YOLO ...