谈谈你对Spring Bean生命周期的理解【面试】

谈谈你对Spring Bean生命周期的理解【面试】SpringBean 生命周期

前言

1、测试结果演示

Spring容器初始化=========================== 五月 16, 2020 3:18:44 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@13b6d03: startup date [Sat May 16 15:18:44 CST 2020]; root of context hierarchy 五月 16, 2020 3:18:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] 【步骤1】执行Bean的无参构造函数 【步骤2】执行Bean的set方法,设置name属性值:coolsummermoon 【步骤2】执行Bean的set方法,设置sex属性值:man 【步骤3】执行BeanNameAware中setBeanName方法,beanName值:iocBeanLifeService 【步骤4】执行BeanClassLoaderAware中setBeanClassLoader,ClassLoader的name = sun.misc.Launcher$AppClassLoader 【步骤5】执行BeanFactoryAware中setBeanFactory,beanFactory中是否包含IocBeanLifeServicetrue 【步骤6】执行EnvironmentAware的setEnvironment方法 【步骤7】执行ResourceLoaderAware的setResourceLoader方法,Resource File Name=applicationContext.xml 【步骤8】执行ApplicationEventPublisherAware中setApplicationEventPublisher方法 【步骤9】执行ApplicationContextAware的setApplicationContext方法,Bean Definition Names=[iocBeanLifeService, org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0, ioc.CustomerBeanPostProcessor#0] 【步骤10】执行BeanPostProcessor中postProcessBeforeInitialization方法,beanName=iocBeanLifeService 【步骤11】执行PostConstruct注解标注的方法 【步骤12】执行InitializingBean的afterPropertiesSet方法 【步骤13】执行配置的init-method 【步骤14】执行BeanPostProcessor的postProcessAfterInitialization方法,beanName=iocBeanLifeService 五月 16, 2020 3:18:45 下午 org.springframework.context.support.AbstractApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@13b6d03: startup date [Sat May 16 15:18:44 CST 2020]; root of context hierarchy Spring容器初始化完毕======================== 从容器中获取Bean IocBeanLifeService{ 
   name='coolsummermoon', sex='man'} Spring容器准备关闭========================== 【步骤15】执行preDestroy注解标注的方法 【步骤16】执行DisposableBean接口的destroy方法 【步骤17】执行配置的destroy-method Spring容器完成关闭=========================== Process finished with exit code 0 

2、结果分析

2.1、上面的结果,我们可以初步分四个阶段:

  1. Bean的实例化阶段
  2. Bean的设置属性阶段
  3. Bean的 初始化阶段
  4. Bean的销毁阶段

2.2、在初始化阶段,有个特别重要的接口BeanPostProcessor,在初始化前、后调用:
在这里插入图片描述
2.3、在设置属性阶段后,postProcessBeforeInitialization方法执行前,会执行很多Aware类型的接口,这种类型接口作用是加载资源到Spring容器中,Aware前面的名字就对应哪种资源,依次加载的是:




  1. BeanNameAware
  2. BeanClassLoaderAware
  3. BeanFactoryAware
  4. EnvironmentAware
  5. ResourceLoaderAware
  6. ApplicationEventPublisherAware
  7. ApplicationContextAware

2.4、初始化方式有三个,分别是:

  1. InitializingBean的afterPropertiesSet方法
  2. PostConstruct注解标注的方法
  3. 配置的init-method

上面的三个方法效果都是一样的,开发中选择其中一种方式就行,一般我们选择2、3方法中的一个。

2.5、容器销毁的方式有三个,分别是:

  1. preDestroy注解标注的方法
  2. DisposableBean接口的destroy方法
  3. 配置的destroy-method

上面的三个方法效果都是一样的,开发中选择其中一种方式就行,一般我们选择1、3方法中的一个。

3、总结

综合前面的代码和分析,现在我们用大白话描述下:

  1. Bean容器找到Spring配置文件中Bean的定义;
  2. Bean容器利用java 反射机制实例化Bean;
  3. Bean容器为实例化的Bean设置属性值;
  4. 如果Bean实现了BeanNameAware接口,则执行setBeanName方法;
  5. 如果Bean实现了BeanClassLoaderAware接口,则执行setBeanClassLoader方法;
  6. 如果Bean实现了BeanFactoryAware接口,则执行setBeanFactory方法;
  7. 如果 ……真的,到这我经常忘记,但前面三个Aware接口肯定能记住;
  8. 如果Bean实现了ApplicationContextAware接口,则执行setApplicationContext方法;
  9. 如果加载了BeanPostProcessor相关实现类,则执行postProcessBeforeInitialization方法;
  10. 如果Bean定义初始化方法(PostConstruct注解、配置init-method、实现了InitializingBean接口),则执行定义的初始化方法;
  11. 如果加载了BeanPostProcessor相关实现类,则执行postProcessAfterInitialization方法;
  12. 当要销毁这个Bean时,如果自定义了销毁方法(PreDestroy注解、配置destroy-method、实现了DisposableBean接口),则执行定义的销毁方法。

结束语

留一个思考题:作用域是单例和原型的Bean,Spring对其生命周期是如何管理的?

欢迎大家关注下方微信公众号,我们一起讨论技术、理财方面的知识,解决生活中遇到的问题。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218427.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午7:17
下一篇 2026年3月18日 上午7:17


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号