当前位置:网站首页>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()
边栏推荐
- Leetcode-160 intersecting linked list
- Lumiprobe lumizol RNA extraction reagent solution
- ES6 summary "suggestions collection" of array methods find(), findindex()
- R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用n.of.lines参数指定显示的病例数
- Huawei cloud experts explain the new features of gaussdb (for MySQL)
- Improve yolov5 with gsconv+slim neck to maximize performance!
- 如何运营好技术相关的自媒体?
- How to find customers for investment attraction in industrial parks
- 力扣每日一题-第32天-1232. 缀点成线
- R language ggplot2 visualization: visualize the line graph and add customized Y-axis label information to the line graph using the labels function
猜你喜欢
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
Solution: you can ping others, but others can't ping me
Clean up system cache and free memory under Linux
隐私沙盒终于要来了
Mysql database of easyclick
Leetcode-141 circular linked list
About enterprise middle office planning and it architecture microservice transformation
Privacy sandbox is finally coming
Improve yolov5 with gsconv+slim neck to maximize performance!
Memo - about C # generating barcode
随机推荐
Three. JS learning - basic operation of camera (learn from)
Create your own NFT collections and publish a Web3 application to show them (Introduction)
Livedata postvalue will "lose" data
Usage and underlying implementation principle of PriorityQueue
Graduation summary
解决方案:可以ping别人,但是别人不能ping我
力扣每日一题-第32天-589.N×树的前序遍历
Stanford, salesforce|maskvit: masked vision pre training for video prediction
How does factor analysis calculate weights?
Viewing the whole ecology of Tiktok from a macro perspective
1、《创建您自己的NFT集合并发布一个Web3应用程序来展示它们》什么是NFT
搭建一个通用监控告警平台,架构上需要有哪些设计
2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
R语言epiDisplay包ordinal.or.display函数获取有序logistic回归模型的汇总统计信息(变量对应的优势比及其置信区间、以及假设检验的p值)、write.csv函数保存csv
如何运营好技术相关的自媒体?
540. Single element in ordered array / 1684 Count the number of consistent strings
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
Privacy sandbox is finally coming
如何在自有APP内实现小程序实现连麦直播
Golang error handling