notes/Spring bean 生命周期.md

20 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Spring bean 生命周期
源码AbstractAutowireCapableBeanFactory.doCreateBean
1. 实例化bean
2. 调用bean的set方法给属性赋值
3. 检查/执行BeanNameAware接口 setName (可选实现BeanNameAware)
检查/执行BeanClassLoaderAware接口 setBeanClassLoader(可选实现BeanClassLoaderAware)
检查/执行BeanFactoryAware接口 setBeanFactory(可选实现BeanFactoryAware)
4. 执行bean后处理器的before 可选自己实现BeanPostProcessor
5. 检查/执行InitializingBean接口afterPropertiesSet 可选实现InitializingBean
6. 初始化bean 可选调用自定义init方法
7. 执行bean后处理器的after可选自己实现BeanPostProcessor
8. 使用bean
9. 检查/执行DisposableBean接口destroy 可选实现DisposableBean
10. 销毁bean 可选在容器关闭时调用自定义destroy方法
spring只对scope为singleton的实例进行完整的生命周期管理如果是prototype只负责到初始化完毕。一旦对象被客户端获取getBeanspring不再负责该bean生命周期。