转:https://blog.csdn.net/liuxilil/article/details/4676088

Spring的生命周期.

  1. 容器启动,实例化所有实现了BeanFactoyPostProcessor接口的类。他会在任何普通Bean实例化之前加载.
  2. 实例化剩下的Bean,对这些Bean进行依赖注入。
  3. 如果Bean有实现BeanNameAware的接口那么对这些Bean进行调用
  4. 如果Bean有实现BeanFactoryAware接口的那么对这些Bean进行调用
  5. 如果Bean有实现ApplicationContextAware接口的那么对这些Bean进行调用
  6. 如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessBeforeInitialization方法
  7. 如果Bean有实现InitializingBean接口那么对这些Bean进行调用
  8. 如果Bean配置有init属性,那么调用它属性中设置的方法
  9. 如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessAfterInitialization方法
  10. Bean正常是使用
  11. 调用DisposableBean接口的destory方法
  12. 调用Bean定义的destory方法

如果从大体上区分值分只为四个阶段

  1. BeanFactoyPostProcessor实例化
  2. Bean实例化,然后通过某些BeanFactoyPostProcessor来进行依赖注入
  3. BeanPostProcessor的调用.Spring内置的BeanPostProcessor负责调用Bean实现的接口: BeanNameAware, BeanFactoryAware, ApplicationContextAware等等,等这些内置的BeanPostProcessor调用完后才会调用自己配置的BeanPostProcessor
  4. Bean销毁阶段

 

以上是自己总结的,没研究过源码,恐有误…作参考用

以下附上验证的代码:

package mislay;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanFactoryPostProcessor;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor {

@Override

public void postProcessBeanFactory(

ConfigurableListableBeanFactory beanFactory) throws BeansException {

System.out.println("--------> begin BeanFactoryPostProcessorTest");

String[] names = beanFactory.getBeanDefinitionNames();

for (String name : names) {

System.out.println("definition bean name:" + name);

}

System.out.println("<--------- end BeanFactoryPostProcessorTest");

}

}

package mislay;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.config.BeanPostProcessor;

public class BeanPostProcessorTest implements BeanPostProcessor {

@Override

public Object postProcessAfterInitialization(Object bean, String beanName)

throws BeansException {

System.out.println("call BeanPostProcessor interface postProcessAfterInitialization method; :" + beanName);

return bean;

}

@Override

public Object postProcessBeforeInitialization(Object bean, String beanName)

throws BeansException {

System.out.println("call BeanPostProcessor interface postProcessBeforeInitialization method ::" + beanName);

if(bean instanceof BeanTest) {

System.out.println("bean instanceof BeanTest");

}

return bean;

}

}

package mislay;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.BeanFactoryAware;

import org.springframework.beans.factory.BeanNameAware;

import org.springframework.beans.factory.DisposableBean;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.beans.factory.config.BeanPostProcessor;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

public class BeanTest implements InitializingBean, DisposableBean,BeanNameAware,BeanFactoryAware,ApplicationContextAware {

@Override

public void afterPropertiesSet() throws Exception {

System.out.println("call InitializingBean interface");

}

@Override

public void destroy() throws Exception {

// TODO Auto-generated method stub

System.out.println("call DisposableBean interface");

}

public void _init() {

System.out.println("call bean init method");

}

public void _destory() {

System.out.println("call bean destory method");

}

public void setSomething(Object something) {

System.out.println("DI call setSomething method");

}

public BeanTest() {

System.out.println("BeanTest create");

}

@Override

public void setBeanName(String name) {

// TODO Auto-generated method stub

System.out.println("call BeanNameAware interface name is:" + name);

}

@Override

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

// TODO Auto-generated method stub

System.out.println("call BeanFactoryAware interface");

}

@Override

public void setApplicationContext(ApplicationContext applicationContext)

throws BeansException {

// TODO Auto-generated method stub

System.out.println("call ApplicationContextAware interface");

}

}

package mislay;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

((AbstractApplicationContext) context).registerShutdownHook();

}

}

 

//下面是输出的内容

//这一段可以看出首先实例化化的BeanFactoryPostProcessor

--------> begin BeanFactoryPostProcessorTest

definition bean name:mislay.BeanFactoryPostProcessorTest

definition bean name:mislay.BeanPostProcessorTest

definition bean name:beanTest

<--------- end BeanFactoryPostProcessorTest

//这一段是Bean的创建以及依赖注入

BeanTest create

DI call setSomething method

//这一段就是内置的BeanPostProcessor调用

call BeanNameAware interface name is:beanTest

call BeanFactoryAware interface

call ApplicationContextAware interface

//这里开始调用自己BeanPostProcessor

call BeanPostProcessor interface postProcessBeforeInitialization method ::beanTest

bean instanceof BeanTest

//穿插着对InitializingBean接口和定义的init方法的调用

call InitializingBean interface

call bean init method

call BeanPostProcessor interface postProcessAfterInitialization method; :beanTest

//上面就结束了对配置的BeanPostProcessor的调用

//最后销毁

call DisposableBean interface

call bean destory method

Spring的生命周期的更多相关文章

  1. spring bean 生命周期和 ? 作用域? spirng bean 相互依赖? jvm oom ? jvm 监控工具? ThreadLocal 原理

    1. spring bean 生命周期 1. 实例化一个bean ,即new 2. 初始化bean 的属性 3. 如果实现接口 BeanNameAware ,调用 setBeanName 4. Bea ...

  2. Spring点滴四:Spring Bean生命周期

    Spring Bean 生命周期示意图: 了解Spring的生命周期非常重要,我们可以利用Spring机制来定制Bean的实例化过程. -------------------------------- ...

  3. spring之生命周期

    1.容器中对的生命周期 spring可以管理 singleton作用域的bean的生命周期,spring可以精确地知道该bean何时被创建,何时被初始化完成,容器合适准备销毁该bean实例. spri ...

  4. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  5. 常见问题:Web/Servlet生命周期与Spring Bean生命周期

    Servlet生命周期 init()初始化阶段 Servlet容器加载Servlet(web.xml中有load-on-startup=1;Servlet容器启动后用户首次向Servlet发请求;Se ...

  6. spring的后置处理器——BeanPostProcessor以及spring的生命周期

    后置处理器的调用时机 BeanPostProcessor是spring提供的接口,它有两个方法——postProcessBeforeInitialization.postProcessAfterIni ...

  7. 大厂高频面试题Spring Bean生命周期最详解

    Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...

  8. Spring Bean生命周期,好像人的一生。。

    大家好,我是老三,上节我们手撸了一个简单的IOC容器五分钟,手撸一个Spring容器!,这节我们来看一看Spring中Bean的生命周期,我发现,和人的一生真的很像. 简单说说IoC和Bean IoC ...

  9. 睡前聊一聊"spring bean 生命周期"

    spring bean 生命周期=实属初销+2个常见接口+3个Aware型接口+2个生命周期接口 实属初销:spring bean生命周期只有四个阶段,即实例化->属性赋值->初始化-&g ...

随机推荐

  1. 从0到有,虚拟机安装centos,然后ssh连接虚拟机

    安装centos 1.下载centos镜像 https://mirrors.aliyun.com/centos/6.8/isos/x86_64/CentOS-6.8-x86_64-bin-DVD1.i ...

  2. VSTO:使用C#开发Excel、Word【14】

    操作workbooks集合可从Application对象的Workbooks属性中获取的Workbooks集合包含当前在应用程序中打开的Workbook对象的集合. 它还具有用于管理开放工作簿,创建新 ...

  3. java中一些对象(po,vo,dao,pojo)等的解释

     一.PO:persistant object 持久对象,可以看成是与数据库中的表相映射的java对象.最简单的PO就是对应数据库中某个表中的一条记录,多个记录可以用PO的集合.PO中应该不包含任何对 ...

  4. servlet邮箱激活验证实例含代码

    也有很多人本来有机会的,他们都拒绝了,不想让自己太累,太麻烦.或者中途被情绪所左右,半途而废了. 成长是有代价的,同样悠闲也是有代价的. 流程: 用户填写相关信息,点击注册按钮 系统先将用户记录保存到 ...

  5. 发布-订阅消息系统Kafka简介

    转载请注明出处:http://www.cnblogs.com/BYRans/ Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式 ...

  6. ios 中pickerView城市选择和UIDatePicker生日选择

    代码详见压缩包

  7. springboot打war包后部署到tomcat后访问返回404错误

    springboot打war包后部署到tomcat后访问返回404错误 1.正常情况下,修改打包方式为war <packaging>war</packaging> 2.启动类继 ...

  8. 使用元类 编写ORM

    元类 一句话: 元类定制类的创建行为 知识点 1.类的创建: python这种动态语言,函数和类的定义,不是编译时定义的,而是运行时动态创建的. Python解释器遇到class定义时,仅仅是扫描一下 ...

  9. Spark访问Hive表

    知识点1:Spark访问HIVE上面的数据 配置注意点:. 1.拷贝mysql-connector-java-5.1.38-bin.jar等相关的jar包到你${spark_home}/lib中(sp ...

  10. MacBook使用笔记2 - 安装windows虚拟机攻略

    转载请标注原链接:http://www.cnblogs.com/xczyd/p/5498878.html 5月初从阿里滚粗,然后失去了公司发的Mac Air.说实话Mac机器确实比windows好用一 ...