In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to master the life cycle of bean in Spring! The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Is the life cycle of bean in Spring short?
In spring, the instance obtained from BeanFactory or ApplicationContext is Singleton, that is, the alias of each Bean is preset to maintain only one instance, instead of generating a new object each time using Singleton mode to generate a single instance. There is no problem for single-threaded programs, but for multithreaded programs, attention must be paid to the issue of Thread-safe. Prevent data asynchrony caused by multiple threads accessing shared resources at the same time.
In spring, however, you can set that each time you specify an alias from BeanFactory or ApplicationContext and get Bean, a new instance is generated: for example:
In spring, the singleton property defaults to true, and only if it is set to false, a new instance will be generated each time the Bean obtained by the alias is specified.
A Bean goes through several execution phases from creation to destruction, if the Bean is generated and managed by BeanFactory (figure 1.1):
Establishment of 1:Bean:
The container looks for the definition information of Bean and instantiates it.
2: attribute injection:
Using dependency injection, Spring configures all Bean properties according to the Bean definition information
SetBeanName () of 3:BeanNameAware:
If the Bean class has an implementation org.springframework.beans.BeanNameAware interface, the factory calls the setBeanName () method of Bean to pass the ID of Bean.
SetBeanFactory () of 4:BeanFactoryAware:
If the Bean class has an implementation org.springframework.beans.factory.BeanFactoryAware interface, the factory calls the setBeanFactory () method into the factory itself.
ProcessBeforeInitialization () of 5:BeanPostProcessors
If there is an org.springframework.beans.factory.config.BeanPostProcessors and Bean association, then its postProcessBeforeInitialization () method will be called.
AfterPropertiesSet () of 6:initializingBean:
If the Bean class has implemented the org.springframework.beans.factory.InitializingBean interface, execute its afterProPertiesSet () method
Define init-method in the 7:Bean definition file:
You can use the "init-method" property to set the method name in the Bean definition file, for example:
If you have the above settings, the initBean () method will be executed at this stage
ProcessaAfterInitialization () of 8:BeanPostProcessors
If any BeanPostProcessors instance is associated with a Bean instance, execute the ProcessaAfterInitialization () method of the BeanPostProcessors instance
At this point, the Bean is ready for use by the application system and will remain in the BeanFactory until it is no longer in use. There are two ways to remove it from BeanFactory (figure 1.2):
Destroy () of 1:DisposableBean
If the Bean class implements the org.springframework.beans.factory.DisposableBean interface when the container is closed, its destroy () method is executed
Define destroy-method in 2:Bean definition file
When the container is closed, you can use the "destroy-method" property to set the method name in the Bean definition file, for example:
If you have the above settings, the destroy () method will be executed at this stage, but it will be slightly different if you use ApplicationContext to generate and manage Bean. If you use ApplicationContext to generate and manage Bean instances, after executing the setBeanFactory () phase of BeanFactoryAware, if there is an implementation of the org.springframework.context.ApplicationContextAware interface on the Bean class, execute its setApplicationContext () method, and then execute the ProcessBeforeInitialization () of BeanPostProcessors and the subsequent process.
The above is the editor for you to share how to master the life cycle of bean in Spring! If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.