Bean的生命周期

52 阅读1分钟

image.png

五步骤版本

1.实例化
2.依赖注入
3.初始化
4.使用bean
5.销毁bean

七步骤版本

1.实例化
2.依赖注入
3.初始化前置处理器postProcessBeforeInitialization\color{red}{初始化前置处理器postProcessBeforeInitialization}
4.初始化
5.初始化后置处理器postProcessAfterInitialization\color{red}{初始化后置处理器 postProcessAfterInitialization}
6.使用bean
7.销毁bean

十步骤版本

1.实例化
2.依赖注入
3.实现BeanNameAware接口\color{red}{实现`BeanNameAware`接口},为实现了各种Aware接口的Bean设置诸如beanName、beanFactory等容器信息
4.初始化前置处理器 postProcessBeforeInitialization
5.实现InitializingBean接口,重写里面的afterPropertiesSet方法\color{red}{实现`InitializingBean`接口,重写里面的`afterPropertiesSet`方法}
6.初始化
7.初始化后置处理器 postProcessAfterInitialization
8.使用bean
9.实现DisposableBean接口,重写里面的destroy方法\color{red}{实现`DisposableBean`接口,重写里面的`destroy`方法}
10.销毁bean