Life Cycle Management of a Spring Bean

Spring Bean 生命周期
本文详细介绍了Spring框架中Bean从实例化到销毁的整个生命周期过程,包括Bean的创建、属性注入、初始化、销毁等关键阶段。
 the sequence of activities that will take place between the time of Bean Instantiation and hand over of the Bean reference to the Client Application:

  1. The Bean Container finds the definition of the Spring Bean in the Configuration file.

---------------------在第2步之前还会检测是否实现了InstantiationAwareBeanPostProcessors,(源码注释:Give BeanPostProcessors a chance to return a proxy instead of the target bean instance),如果此postProcessBeforeInstantiation不返回null,后续doCreateBean方法中的aware方法,ini方法都不会执行----------

  1. The Bean Container creates an instance of the Bean using Java Reflection API.
  2. If any properties are mentioned, then they are also applied. If the property itself is a Bean, then it is resolved and set.

------------------------当执行第四步骤时候,bean在JVM中已经实例化,且完成了spring初始化的属性注入--------------

  1. If the Bean class implements the BeanNameAware interface, then the setBeanName()method will be called by passing the name of the Bean. (bean感知自己在factory中的名字)
  2. If the Bean class implements the BeanClassLoaderAware interface, then the method setBeanClassLoader() method will be called by passing an instance of the ClassLoader object that loaded this bean.
  3. If the Bean class implements the BeanFactoryAware interface, then the method setBeanFactory() will be called by passing an instance of BeanFactory object. (bean感知自己所在在的factory)
  4. If there are any BeanPostProcessors object associated with the BeanFactory that loaded the Bean, then the method postProcessBeforeInitialization() will be called even before the properties for the Bean are set.
  5. If the Bean class implements the InitializingBean interface, then the method afterPropertiesSet() will be called once all the Bean properties defined in the Configuration file are set.
  6. If the Bean definition in the Configuration file contains a 'init-method' attribute, then the value for the attribute will be resolved to a method name in the Bean class and that method will be called.
  7. The postProcessAfterInitialization() method will be called if there are any Bean Post Processors attached for the Bean Factory object.
  8. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
  9. If the Bean definition in the Configuration file contains a 'destroy-method'attribute, then the corresponding method definition in the Bean class will be called.



reference: https://javabeat.net/life-cycle-management-of-a-spring-bean





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值