1、Bean的建立是由BeanFactory读取Bean定义文件,并生成各个实例
2、Setter注入,即执行Bean的属性依赖注入
3、通过BeanNameAware的setBeanName()方法设置名字
4、再经过BeanFactoryAware的setBeanFactory()方法
5、再经过BeanPostProcessor的processBeforeInitialization()进行初始化。在Bean初始化之前,只要有关联的processor,都会执行这个实例的processBeforeInitialization()方法
6、InitializingBean的afterPropertiesSet(),只要实现了该接口,就会执行其afterPropertiesSet()方法
7、在Bean定义文件中定义init-method
8、再通过DisposableBean的destroy()关闭容器,在容器关闭时,可以使用在Bean定义文件中定义的“destory-method”方法
简单来说,Spring Bean的生命周期就是:
(1)实例化构造函数构造对象(必要的)
(2)装配为属性赋值(非必要的)
(3)回调(容器-控制类和组件-回调类)(非必要的)
(4)初始化方法(init-method="xxx")
(5)就绪
(6)销毁方法(destroy-method="xxx")