当前位置:网站首页>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()
边栏推荐
- Write it down once Net travel management background CPU Explosion Analysis
- 6款红黄黑榜摄像头评测:谁最安全?谁画质好?从此让你不再踩雷
- Leetcode203 移除链表元素
- 1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- Five degrees easy chain enterprise app is newly upgraded
- Li Kou daily question - Day 32 -1232 Dotted line
- Excel之VBA简单宏编程
- Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
- 【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
猜你喜欢

Improve yolov5 with gsconv+slim neck to maximize performance!

Lumiprobe bifunctional crosslinker sulfo cyanine 5 bis NHS ester

Leetcode-141环形链表

Altair HyperWorks 2022软件安装包和安装教程

Livedata postvalue will "lose" data

6款红黄黑榜摄像头评测:谁最安全?谁画质好?从此让你不再踩雷

Leetcode-160相交链表

Lumiprobe non fluorescent alkyne EU (5-ethynyluridine)

Leetcode-83 delete duplicate elements in the sorting linked list

【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
随机推荐
Qfile read / write file operation in QT
AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
About enterprise middle office planning and it architecture microservice transformation
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
Leetcode203 移除链表元素
Openai video pre training (VPT): action learning based on watching unmarked online videos
Create your own NFT collections and publish a Web3 application to show them (Introduction)
1380. Lucky number in matrix / 1672 Total assets of the richest customers
GAMES202作业0-环境搭建过程&解决遇到的问题
Three. JS learning - basic operation of camera (learn from)
R语言使用dplyr包的transmute函数计算dataframe数据中的指定数据列的移动窗口均值、使用ggplot2包可视化移动均值与原始数据的折线图
golang 错误处理
The R language cartools package divides the data, the scale function scales the data, the KNN function of the class package constructs the k-nearest neighbor classifier, and the table function calcula
数据库基础:select基本查询语句
R语言使用epiDisplay包的aggregate函数将数值变量基于因子变量拆分为不同的子集,计算每个子集的汇总统计信息
Thread forced join, thread forced join application scenarios
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
Example explanation: move graph explorer to jupyterlab
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
太爱速M源码搭建,巅峰小店APP溢价寄卖源码分享