SpringBean 生命周期
2023-01-12 17:35:52 0 举报
AI智能生成
SpringBean生命周期
作者其他创作
大纲/内容
什么是Spring Bean的生命周期
元信息阶段
元信息配置
资源
XML
Properties
注解
API
元信息解析
面向资源
BeanDefinitionReader
XML解析器
面向注解
AnnotatedBeanDefinitionReader
Spring Bean注册
BeanDefinition注册接口
BeanDefinitionRegistry
Spring BeanDefinition合并阶段 doGetBean
父子BeanDefinition合并
当前BeanFactory查找
层次性BeanFactory查找
Class加载阶段 createBean
参考
ClassLoader类加载
Java Security安全加载控制
ConfigurableBeanFactory临时ClassLoader
Bean实例化前阶段 createBean
InstantiationAwareBeanPostProcessor#postProcessBeforeInstantation
Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
实例化阶段 doCreateBean
Bean实例化
传统
实例化策略 - InstantiationStrategy
构造器
Bean实例化之后 populateBean
填充Bean
InstatiationAwareBeanPostProcessor#postProcessAfterInstantiation
Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
state of the bean before properties are set. This can be used, for example,
to support styles of field injection.
state of the bean before properties are set. This can be used, for example,
to support styles of field injection.
Bean属性赋值前阶段 populateBean
InstantiationAwareBeanPostProcessor#postProcessProperties
赋值给bw
初始化阶段 initializeBean
接口回调阶段
invokeAwareMethods
BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
applyBeanPostProcessorsBeforeInitialization
ApplicationContextAwareProcessor
postProcessBeforeInitialization
PostContruct注解
InitDestroyAnnotationBeanPostProcessor#postProcessBeforeInitialization
如果实现InitializingBean
afterPropertiesSet
invokeCustomInitMethod
applyBeanPostProcessorsAfterInitialization
postProcessAfterInitialization
初始化完成阶段
SmartInitializingSingleton
afterSingletonsInstantiated
BeanFactory显示回调
通常在ApplicationContext场景使用
将极硬注册的 BeanDefinition初始化成Bean
销毁阶段
DestructionAwareBeanPostProcessor
postProcessBeforeDestruction
PreDestroy
DispoableBean
Destory
customDestoryMethod
GC阶段
Spring Aware
总揽
BeanFactoryPostProcessor
AbstractApplicationContext.refresh()
扩展BeanFactory
BeanPostProcessor
在这个bean的生命周期里进行回调等操作
实例化前、后
初始化前、后
ApplicationContextAware接口回调也是基于此
收藏
收藏
0 条评论
下一页