当前位置:网站首页>The Bean's life cycle
The Bean's life cycle
2022-08-01 06:02:00 【Hypnos924】
Bean life cycle
Bean's life cycle is generally divided into 4 stages
Instantation---->Property assignment Populate---->Initialization---->Destruction Destriction
In these 4 stages, it is divided into several small steps, let's go into detail below
First step (instantiating the bean)
There are two cases here. If the BeanFactory container is used, when an uninitialized bean is requested from the container, or an uninitialized dependency needs to be injected into the dependency, the container will call createBean for instantiation
For using the ApplicationContext container, when the container is started, all beans are instantiated by obtaining the information in the BeanDefinition object
Part 2(set to detailed properties)
The instantiated object is encapsulated in the BeanWrapper object, and then Spring will complete the property setting and dependency injection according to the information in the BeanDefinition and the interface for setting properties provided by the BeanWrapper
Step 3 (handling the Aware interface)
Spring will detect whether the object implements the lxxxAware interface. Through the Aware type interface, we can get some resources of the Spring container
This is divided into the following situations
If the bean implements the BeanNameAware interface, the setBeanName(String beanld) method it implements will be called, and the name of the bean is passed in
If the Bean implements the BeanClassLoaderAware interface, call the setBeanCLassLoader() method and pass in the ClassLoader object instance
If the Bean implements the BeanFactoryAware interface, it will be called to implement the setBeanFactory() method, and the Spring factory itself will be passed
If this bean implements the ApplicationContextAware interface, the setApplicationContext(ApplicationContext) method will be called and the Spring context will be passed in
Step 4(BeanPostProcessor preprocessing)
If some custom preprocessing is performed on the bean, then the bean can implement the BeanPostProcessor interface, then the PostProcessorBeaforeInitialization(Object obj,String s) method will be called
Step 5(InitializingBean)
If you implement the InitializingBean interface, you only want the afterPropertiesSet() method
Step 6(init-method)
If the bean is configured with the init-method attribute in the Spring configuration file, its configured initialization method will be called automatically
The seventh step BeanPostProcessor post-processing
After the above steps, the bean has been created correctly, and the bean can be used after that
If the bean implements the BeanPostProccessor interface, the postProccessAfterInitialization(object obj, String s) method will be called; since this method is called at the end of bean initialization, it can be applied to memory or caching techniques
Step 8DisposableBean
When the bean is not needed, it will go through the cleanup phase. If the bean implements the DisposableBean interface, it will call the Destroy() method of other implementations
Step 9destory-method
Finally, if the destruction-method attribute is configured in the Spring configuration of this Bea, its configured destruction method will be called automatically
边栏推荐
猜你喜欢
随机推荐
【翻译】确保云原生通信的安全:从入口到服务网及更远的地方
AspNet.WebApi.Owin 自定义Token请求参数
Robot_Framework:关键字
Use controls as brushes to get bitmap code records
导致锁表的原因及解决方法
Seleniu:元素常用操作
奇葩问题 npm install 报错 gyp ERR
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
微信小程序接口调用凭证(获取token)auth.getAccessToken接口开发
轻量级的VsCode为何越用越大?为什么吃了我C盘10G?如何无痛清理VsCode缓存?手把手教你为C盘瘦身
Bean的生命周期
七、MFC序列化机制和序列化类对象
深度比较两个对象是否相同
Robot_Framework:断言
uva12326
uva10825
Motion analysis and parameter optimization of crank-slider mechanism
uva10825
Malicious attacks on mobile applications surge by 500%
曲柄滑块机构运动分析和参数优化









