一、生命周期流程图:

  Spring Bean的完整生命周期从创建Spring容器开始,直到最终Spring容器销毁Bean,这其中包含了一系列关键点

假设一个Bean实现了所有的接口,大的概况一下Bean的生命周期:

1.实例化BeanFactoryPostProcessor:处理的对象是BeanFactory级别

2.实例化BeanPostProcessor实现类

3.实例化InstantiationAwareBeanPostProcessorAdapter实现类,注:该类是BeanPostProcessor的扩展

4.执行InstantiationAwareBeanPostProcessorAdapter类的postProcessBeforeInstantiation方法

5.Bean的构造方法

6.执行InstantiationAwareBeanPostProcessorAdapter类的postProcessPropertyValues

7.为Bean注入属性,即依赖注入

8.调用BeanNameAware的setBeanName方法

9.调用BeanNameAware的setBeanFactory方法

10.执行BeanPostProcessor的后置处理器,postProcessAfterInitialization方法

11.调用InitializingBean的afterPropertiesSet方法

12.调用bean的init-method初始化方法

13.执行BeanPostProcessor的postProcessAfterInitialization

14.执行InstantiationAwareBeanPostProcessorAdapter的后置方法,postProcessAfterInitialization

15.Bean的使用

16.调用DiposibleBean的destory方法

17.调用bean指定的destory-method方法

为了上述流程的正确性,我们可以用代码来测试一下:

测试Bean - StudentBean类:

/**
* 测试生命周期的Bean
* Created by kxm
*/
public class StudentBean implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware {
private String name;
private int age; private String beanName; //实现了BeanNameAware接口,Spring可以将BeanName注入该属性中
private BeanFactory beanFactory; //实现了BeanFactory接口,Spring可将BeanFactory注入该属性中 public StudentBean(){
System.out.println("【Bean构造方法】学生类的无参构造方法");
} @Override
public String toString() {
return "StudentBean{" +
"name='" + name + '\'' +
", age=" + age +
", beanName='" + beanName + '\'' +
'}';
} public String getName() {
return name;
} public void setName(String name) {
System.out.println("【set注入】注入学生的name属性");
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
System.out.println("【set注入】注入学生的age属性");
this.age = age;
} /**
* 自己编写的初始化方法
*/
public void myInit(){
System.out.println("【init-method】调用init-method属性配置的初始化方法");
} /**
* 自己编写的销毁方法
*/
public void myDestroy(){
System.out.println("【destroy-method】调用destroy-method属性配置的销毁方法");
} /**
* BeanFactoryAware接口的方法
* @param beanFactory
* @throws BeansException
*/
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
System.out.println("【BeanFactoryAware接口】调用BeanFactoryAware的setBeanFactory方法得到beanFactory引用");
} /**
* BeanNameAware接口的方法
* @param name
*/
public void setBeanName(String name) {
this.beanName = name;
System.out.println("【BeanNameAware接口】调用BeanNameAware的setBeanName方法得到Bean的名称");
} /**
* InitializingBean接口的方法
* @throws Exception
*/
public void afterPropertiesSet() throws Exception {
System.out.println("【InitializingBean接口】调用InitializingBean接口的afterPropertiesSet方法");
} /**
* DisposableBean接口的方法
* @throws Exception
*/
public void destroy() throws Exception {
System.out.println("【DisposableBean接口】调用DisposableBean接口的destroy方法");
}
}

MyBeanFactoryPostProcessor工厂处理器类:

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    public MyBeanFactoryPostProcessor() {
System.out.println("【BeanFactoryPostProcessor接口】调用BeanFactoryPostProcessor实现类构造方法");
} /**
* 重写BeanFactoryPostProcessor接口的postProcessBeanFactory方法,可通过该方法对beanFactory进行设置
*/
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
System.out.println("【BeanFactoryPostProcessor接口】调用BeanFactoryPostProcessor接口的postProcessBeanFactory方法");
}
}

MyInstantiationAwareBeanPostProcessor类:

/**
* 一般情况下,当我们需要实现InstantiationAwareBeanPostProcessor接口时,是通过继承Spring框架中InstantiationAwareBeanPostProcessor接口实现类
* InstantiationAwareBeanPostProcessorAdapter这个适配器类来简化我们实现接口的工作
*/
public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter { public MyInstantiationAwareBeanPostProcessor() {
System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor构造方法");
} /**
* 实例化Bean之前调用
*/
public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法");
return null;
} /**
* 实例化Bean之后调用
*/
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInitialization方法");
return bean;
} /**
* 设置某个属性时调用
*/
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法");
return pvs;
}
}

MyBeanPostProcessor类:

/**
* Created by kxm
*/
public class MyBeanPostProcessor implements BeanPostProcessor { public MyBeanPostProcessor(){
System.out.println("【BeanPostProcessor接口】调用BeanPostProcessor的构造方法");
} public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("【BeanPostProcessor接口】调用postProcessBeforeInitialization方法,这里可对"+beanName+"的属性进行更改。");
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("【BeanPostProcessor接口】调用postProcessAfterInitialization方法,这里可对"+beanName+"的属性进行更改。");
return bean;
}
}

XML配置:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- 生命周期测试 -->
<!--配置Bean的后置处理器-->
<bean id="beanPostProcessor" class="com.my.spring.life.MyBeanPostProcessor">
</bean> <!--配置instantiationAwareBeanPostProcessor-->
<bean id="instantiationAwareBeanPostProcessor" class="com.my.spring.life.MyInstantiationAwareBeanPostProcessor">
</bean> <!--配置BeanFactory的后置处理器-->
<bean id="beanFactoryPostProcessor" class="com.my.spring.life.MyBeanFactoryPostProcessor">
</bean> <bean id="studentBean" class="com.my.spring.life.StudentBean" init-method="myInit"
destroy-method="myDestroy" scope="singleton">
<property name="name" value="yanxiao"></property>
<property name="age" value="21"></property>
</bean>
</beans>

测试类:

public class TestCyclelife {

	/**
* Created by yanxiao on 2016/8/1.
*/
public static void main(String[] args){
System.out.println("--------------【初始化容器】---------------"); ApplicationContext context = new ClassPathXmlApplicationContext("Springlife.xml");
System.out.println("-------------------【容器初始化成功】------------------");
//得到studentBean,并显示其信息
StudentBean studentBean = context.getBean("studentBean",StudentBean.class);
System.out.println(studentBean); System.out.println("--------------------【销毁容器】----------------------");
((ClassPathXmlApplicationContext)context).registerShutdownHook();
}
}

测试结果如下:

符合上述的Bean的生命周期,至此,Bean的生命周期问题已基本解决,不过还等待着你继续去深入。

Spring Bean的生命周期 ---附详细流程图及测试代码的更多相关文章

  1. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  2. Spring Bean的生命周期,《Spring 实战》书中的官方说法

    连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...

  3. Spring Bean的生命周期相关博客

    最近得面试题一直 问 Spring 得生命周期,鉴于自己还未阅读过源码 所以只能是自己 背一波了.属实不懂硬背得作用,但是无奈被各位面试官打败了.等以后有时间了 一定要阅读几遍spring的 源码 有 ...

  4. Spring Bean的生命周期详解(转)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  5. Spring动态代理及Spring Bean的生命周期

    数组添加值 public class DiTest { /** * 数组 */ private String [] arrays; /** * List:集合 */ private List<I ...

  6. 谈谈Spring bean的生命周期(一)

    简介 本片文章主要讲Spring IOC容器中 bean 的生命周期 Spring bean 生命周期 Spring 中bean的声明周期 可以分为如下4个阶段: 实例化阶段--Instantiati ...

  7. spring bean的生命周期

    掌握好spring bean的生命周期,对spring的扩展大有帮助.  spring bean的生命周期(推荐看)  spring bean的生命周期

  8. Spring学习手札(四)谈谈Spring Bean的生命周期及作用域

    在Spring中,那些组成应用程序的主体以及由Spring IoC容器所管理的对象,被称之为Bean.Bean与应用程序中其他对象(比如自己创建类)的区别就是,Bean是由IoC容器创建于销毁的.在S ...

  9. Spring(三)--Spring bean的生命周期

    Spring bean的生命周期 ApplicationContext Bean生命周期流程 1.需要的实体类 ackage com.xdf.bean; import org.springframew ...

随机推荐

  1. 果然学习好是有道理的,学习Mysql与正则表达式笔记

    正则表达式是用来匹配文本的特殊的字符集合,将一个正则表达式与文本串进行比较,Mysql中用where子句提供支持,正则表达式关键字:regexp1.使用‘|’匹配两个串中的一个 2.使用‘[]’匹配几 ...

  2. 【Java】错误: 需要class, interface或enum

    今天在用cmd实现mvn package操作时跳出来的报错! 网上搜索到的结论是因为编码问题而产生的,具体原因就不深究了 要详细了解可以查看以下链接https://blog.csdn.net/qq_3 ...

  3. Mysql使用xtrabackup备份失败处理

    在生产环境中使用的是xtrabackup,对mysql进行备份,每天0点开始备份,周日是全量备份,其他时间是基于周日做的增量备份,通过脚本实现,每天备份完成后会发送短信,突然有一天,备份全部失败,手动 ...

  4. .Net Core Configuration源码探究

    前言     上篇文章我们演示了为Configuration添加Etcd数据源,并且了解到为Configuration扩展自定义数据源还是非常简单的,核心就是把数据源的数据按照一定的规则读取到指定的字 ...

  5. Selenium+java - Edge浏览器启动

    写在前面 随着win10系统的普及,使得Edge浏览器得到广泛使用.从自动化角度看,自然微软也一直不断提供着支持服务,系统版本更新,对应的Edge浏览器版本也在更新,当然对应的驱动版本也会发生变化. ...

  6. 入门大数据---基于Zookeeper搭建Kafka高可用集群

    一.Zookeeper集群搭建 为保证集群高可用,Zookeeper 集群的节点数最好是奇数,最少有三个节点,所以这里搭建一个三个节点的集群. 1.1 下载 & 解压 下载对应版本 Zooke ...

  7. 【MonogDB帮助类】

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using MongoDB; ...

  8. Python进阶之浅谈内置方法

    目录 有序or无序和可变or不可变 数字类型内置方法 整形 浮点型 字符串类型内置方法 有序or无序和可变or不可变 有序:有索引 无序:无索引 可变:变量值变,id不变 不可变:变量值变,id也变 ...

  9. object detection api调参详解(兼SSD算法参数详解)

    一.引言 使用谷歌提供的object detection api图像识别框架,我们可以很方便地重新训练一个预训练模型,用于自己的具体业务.以我所使用的ssd_mobilenet_v1预训练模型为例,训 ...

  10. 深入理解JVM(③)Java模块化系统

    前言 JDK9引入的Java模块化系统(Java Platform Module System ,JPMS)是 对Java技术的一次重要升级,除了像之前JAR包哪有充当代码的容器之外,还包括: 依赖其 ...