当前位置:网站首页>bean的生命周期核心步骤总结
bean的生命周期核心步骤总结
2022-07-01 18:36:00 【51CTO】
BFPP与BPP说明
后续行文为了方便,用BFPP代指BeanFactoryPostProcessor,用BPP代指BeanPostProcessor。
xml/注解方式开启spring
容器创建
refresh() 中的obtainFreshBeanFactory()
创建容器
创建的是DefaultListableBeanFactory
设置容器属性值
加载bean定义信息
包含非常多的重名重载方法,建议debug调试查看,不然容易晕。
xlm转换成document
document解析成element
parseDefaultElement(ele, delegate)
默认的指bean
delegate.parseCustomElement(ele)
自定义的指以context、aop等开头或者自定义的标签。
以上两种标签解析后的结果都封装在BeanDefinition中,获取的是GenericBeanDefinition,后续实例化用的是RootBeanDefinition,需要进行一个合并。
给容器工厂设置属性值
prepareBeanFactory(beanFactory);
设置类加载器等
postProcessBeanFactory
空方法,供子类实现。
实例化和执行BFPP
调用和执行BFPP,操作的对象是BeanFactory,可以创建、修改或者引入其他的BeanDefinition,里面有个非常重要的类,ConfigurationClassPostProcessor
ConfigurationClassPostProcessor
完成@Bean,@[email protected],@Import,@ImportSource等注解的解析
完成BPP的实例化与注册
registerBeanPostProcessors(beanFactory),完成BPP的注册与实例化,方便后续在创建bean过程完成之后调用before/after方法堆bean进行增强。
完成自定义对象的创建
finishBeanFactoryInitialization(beanFactory)
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
将需要创建的对象放到list中,逐一进行创建。
getMergedLocalBeanDefinition会将上文提到的GenericBeanDefinition转换成RootBeanDefinition
for ( String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition( beanName);
if ( ! bd. isAbstract() && bd. isSingleton() && ! bd. isLazyInit()) {
if ( isFactoryBean( beanName)) {
Object bean = getBean( FACTORY_BEAN_PREFIX + beanName);
if ( bean instanceof SmartFactoryBean <?> smartFactoryBean && smartFactoryBean. isEagerInit()) {
getBean( beanName);
}
}
else {
getBean( beanName);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
getBean
doGetBean
先从容器中找,如果没有再创建
createBean
doCreataBean
createBeanInstance
创建bean有多重方式
getInstanceSupplier(Supplier方式创建)
instantiateUsingFactoryMethod(FactoryMethod方式创建)
instantiateBean(默认的,反射方式创建)
instantiateBean
其实还有个BPP代理模式创建对象,但不是在createBeanInstance,而是在前面的步骤
最终调用反射进行bean的创建
注册生命周期接口
也就是applyMergedBeanDefinitionPostProcessors
// Allow post-processors to modify the merged bean definition.
synchronized ( mbd. postProcessingLock) {
if ( ! mbd. postProcessed) {
try {
applyMergedBeanDefinitionPostProcessors( mbd, beanType, beanName);
} catch ( Throwable ex) {
throw new BeanCreationException( mbd. getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd. postProcessed = true;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
再此步骤完成@PostConstruct、@PreDestory、@Resource、@Autowire、@Value注解的解析工作。
填充属性populateBean
创建依赖的对象,循环依赖问题再此触发
进行初始化initializeBean
执行invokeAwareMethods
private void invokeAwareMethods( String beanName, Object bean) {
if ( bean instanceof Aware) {
if ( bean instanceof BeanNameAware) {
(( BeanNameAware) bean). setBeanName( beanName);
}
if ( bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if ( bcl != null) {
(( BeanClassLoaderAware) bean). setBeanClassLoader( bcl);
}
}
if ( bean instanceof BeanFactoryAware) {
(( BeanFactoryAware) bean). setBeanFactory( AbstractAutowireCapableBeanFactory. this);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
执行BPP的before方法
ApplicationAwarePostProcessors、CommonAnnotationBeanPostProcessor等
invokeInitMethods
如果实现了InitializingBean接口,需要调用afterPropertiesSet,最后一次修改属性值
执行用户自定义的init-method(invokeCustomInitMethod)
执行BPP的after方法
aop功能再此实现。
销毁对象
destroyBeans()
边栏推荐
- Mise en place d'une plate - forme générale de surveillance et d'alarme, quelles sont les conceptions nécessaires dans l'architecture?
- Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
- A wonderful time to buy and sell stocks
- ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
- Popular science: what does it mean to enter the kernel state?
- R语言caTools包进行数据划分、scale函数进行数据缩放、class包的knn函数构建K近邻分类器、table函数计算混淆矩阵
- 因子分析怎么计算权重?
- Write it down once Net travel management background CPU Explosion Analysis
- Blue Bridge Cup real problem: word analysis
- 如何在自有APP内实现小程序实现连麦直播
猜你喜欢
Evaluation of 6 red, yellow and black list cameras: who is the safest? Who has good picture quality? From now on, let you no longer step on thunder
Localization through custom services in the shuttle application
OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习
Search 2D matrix 2
信度系数低怎么办?信度系数具体怎么算?
每周推薦短視頻:警惕“現象”與“問題”相互混淆
Leetcode-128 最长连续序列
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Basic knowledge and commands of disk
必看,时间序列分析
随机推荐
宏观视角看抖音全生态
Leetcode-128 最长连续序列
Solution: you can ping others, but others can't ping me
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
R语言使用dplyr包的transmute函数计算dataframe数据中的指定数据列的移动窗口均值、使用ggplot2包可视化移动均值与原始数据的折线图
必看,时间序列分析
Blue Bridge Cup real problem: word analysis
Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
Leetcode203 移除链表元素
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用pch参数自定义指定点图数据点的形状
Regular expression
Bernoulli distribution (a discrete distribution)
Mise en place d'une plate - forme générale de surveillance et d'alarme, quelles sont les conceptions nécessaires dans l'architecture?
R语言ggplot2可视化:gganimate创建动态柱状图动画(gif)、在动画中沿给定维度逐步显示柱状图、enter_grow函数和enter_fade函数控制运动内插退出(渐变tweening)
R language epidisplay package ordinal or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence interval correspo
Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
研究了11种实时聊天软件,我发现都具备这些功能…
Financial judgment questions
How to change guns for 2D characters
Relational database management system of easyclick