当前位置:网站首页>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()
边栏推荐
- 每周推薦短視頻:警惕“現象”與“問題”相互混淆
- 华为云专家详解GaussDB(for MySQL)新特性
- 毕业总结
- R语言ggplot2可视化:gganimate包transition_time函数创建动态散点图动画(gif)、shadow_wake函数配置动画的渐变效果(gradual falloff)拖尾效应
- 主成分之综合竞争力案例分析
- Reading notes series "modern methods of C language programming" -- Chapter 2
- 每周推荐短视频:警惕“现象”与“问题”相互混淆
- Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯
- Three. JS learning - basic operation of camera (learn from)
- 11、用户、组和权限(1)
猜你喜欢

AI 训练速度突破摩尔定律;宋舒然团队获得RSS 2022最佳论文奖

What if the reliability coefficient is low? How to calculate the reliability coefficient?

A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database

3、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》在本地铸造 NFT
![[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter](/img/bf/f8759195ea67832835e84c851b5e81.jpg)
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter

The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup

docker 部署mysql8.0

540. Single element in ordered array / 1684 Count the number of consistent strings

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

What designs are needed in the architecture to build a general monitoring and alarm platform
随机推荐
Roll out! Enlightenment!
Halcon图片标定,使得后续图片处理过后变成与模板图片一样
Vidéos courtes recommandées chaque semaine: méfiez - vous de la confusion entre « phénomène » et « problème »
Leetcode-21 combines two ordered linked lists
研究了11种实时聊天软件,我发现都具备这些功能…
Leetcode-141 circular linked list
Write an open source, convenient and fast database document query and generation tool with WPF
A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses PCH parameters to customize the shape o
Livedata postvalue will "lose" data
Lumiprobe非荧光炔烃丨EU(5-乙炔基尿苷)
1380. Lucky number in matrix / 1672 Total assets of the richest customers
Leetcode-141环形链表
Financial judgment questions
Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
Localization through custom services in the shuttle application
Calculation of intersection of two line segments
Popular science: what does it mean to enter the kernel state?
必看,时间序列分析
Leetcode-128 最长连续序列