当前位置:网站首页>Summary of the core steps in the life cycle of beans
Summary of the core steps in the life cycle of beans
2022-07-01 18:55:00 【51CTO】
BFPP And BPP explain
The following text is for convenience , use BFPP Generation refers to BeanFactoryPostProcessor, use BPP Generation refers to BeanPostProcessor.
xml/ Annotation mode on spring
Container to create
refresh() Medium obtainFreshBeanFactory()
Create a container
Created is DefaultListableBeanFactory
Set container attribute values
load bean Defining information
Contains many overloaded methods with duplicate names , Suggest debug Debug view , Otherwise, it's easy to faint .
xlm convert to document
document It can be interpreted as element
parseDefaultElement(ele, delegate)
The default refers to bean
delegate.parseCustomElement(ele)
Customized refers to context、aop Wait for the beginning or customized label .
The results of the parsing of the above two tags are encapsulated in BeanDefinition in , What you get is GenericBeanDefinition, The subsequent instantiation uses RootBeanDefinition, A merge is needed .
Set attribute values for the container factory
prepareBeanFactory(beanFactory);
Set class loader, etc
postProcessBeanFactory
Empty method , For subclasses to implement .
Instantiate and execute BFPP
Call and execute BFPP, The object of operation is BeanFactory, You can create 、 Modify or introduce others BeanDefinition, There is a very important class ,ConfigurationClassPostProcessor
ConfigurationClassPostProcessor
complete @Bean,@[email protected],@Import,@ImportSource And so on
complete BPP Instantiation and registration of
registerBeanPostProcessors(beanFactory), complete BPP Registration and instantiation of , Convenient for subsequent creation bean After the procedure is completed, call before/after Method heap bean enhanced .
Complete the creation of custom objects
finishBeanFactoryInitialization(beanFactory)
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
Put the object to be created into list in , Create one by one .
getMergedLocalBeanDefinition The above mentioned GenericBeanDefinition convert to 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
First look in the container , If there is no more creation
createBean
doCreataBean
createBeanInstance
establish bean There are multiple ways
getInstanceSupplier(Supplier Way to create )
instantiateUsingFactoryMethod(FactoryMethod Way to create )
instantiateBean( default , Create by reflection )
instantiateBean
There's actually another one BPP Proxy mode creates objects , But not in createBeanInstance, But in the previous steps
Finally, reflection is called bean The creation of
Register the lifecycle interface
That is to say 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.
Finish this step @PostConstruct、@PreDestory、@Resource、@Autowire、@Value Annotation parsing work .
Fill properties populateBean
Create dependent objects , The circular dependency problem is triggered again
To initialize initializeBean
perform 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.
perform BPP Of before Method
ApplicationAwarePostProcessors、CommonAnnotationBeanPostProcessor etc.
invokeInitMethods
If it's done InitializingBean Interface , Need to call afterPropertiesSet, Modify the attribute value for the last time
Perform user-defined init-method(invokeCustomInitMethod)
perform BPP Of after Method
aop This function is realized .

Destroy object
destroyBeans()
边栏推荐
- Memo - about C # generating barcode
- 1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
- Case study on comprehensive competitiveness of principal components
- 毕业总结
- Go语言自学系列 | go语言数据类型
- How to find the optimal learning rate
- AppGallery Connect场景化开发实战—图片存储分享
- How does factor analysis calculate weights?
- 2020,最新手机号码手机验证正则表达式,持续更新
- R语言使用dplyr包的transmute函数计算dataframe数据中的指定数据列的移动窗口均值、使用ggplot2包可视化移动均值与原始数据的折线图
猜你喜欢

搭建一个通用监控告警平台,架构上需要有哪些设计

How to find the optimal learning rate

Three.js学习-相机Camera的基本操作(了解向)

如何使用物联网低代码平台进行个人设置?

Livedata postvalue will "lose" data

Privacy sandbox is finally coming

Leetcode203 移除链表元素

Example explanation: move graph explorer to jupyterlab

How does factor analysis calculate weights?

Solution: you can ping others, but others can't ping me
随机推荐
透过华为军团看科技之变(六):智慧公路
Leetcode-160相交链表
Golang error handling
Go语言自学系列 | go语言数据类型
Stanford, salesforce|maskvit: masked vision pre training for video prediction
R language ggplot2 visualization: gganimate creates a dynamic histogram animation (GIF), and displays the histogram and enter step by step along a given dimension in the animation_ Growth function and
R语言epiDisplay包ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、write.csv函数保存csv
Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
用WPF写一款开源方便、快捷的数据库文档查询、生成工具
Leetcode-83 删除排序链表中重复的元素
R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的Y轴标签信息(customize y axis label)
Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester
540. Single element in ordered array / 1684 Count the number of consistent strings
解决方案:可以ping别人,但是别人不能ping我
2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
AI 训练速度突破摩尔定律;宋舒然团队获得RSS 2022最佳论文奖
Halcon image calibration enables subsequent image processing to become the same as the template image
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用pch参数自定义指定点图数据点的形状
golang 错误处理
Mysql database of easyclick