Spring
May 27, 2020
M01 Q07 Can you describe the lifecycle of a Spring Bean in an ApplicationContext?
To analyze the lifecycle of the bean in Application Context first we need to check how the bean definitions are created.
- Beans Definitions are created based on Spring Bean Configuration. To create a bean definition they need to be read from Spring Bean Configuration. Spring Bean Configuration can be supplied via XML or annotation config.
 - BeanFactoryPostProcessors are invoked. All of the bean definitions can be post-processed by the objects that are implementing the 
BeanFactoryPostProcessorinterface. On this level Spring gives you an ability to access any of the bean definition. 
After that, you have the bean definitions and Spring will start creating the Spring bean objects.
- The instance of the bean is created
 - Properties and dependencies are set.
 BeanPostProcessor::postProcessBeforeInitializationgets called. This is a chance to customize the bean object before it will be used.@PostConstructmethod gets called.InitializingBean::afterPropertiesSetmethod gets called.@Bean(initMethod)method gets called.BeanPostProcessor::postProcessAfterInitializationgets called.
Bean will be destroyed when the Application Context will get closed.