I was studying the life cycle of the bean in a Spring application. The Spring container performs several step in the bean creation process before making it available to the use for the application.
I created a small algorithm detailing the creation process below:
Similarly when the application is shutting down, it will result in the container being destroyed too. Naturally the beans are also approaching the end of their lifecycle. Before that there are a few steps:
I created a small algorithm detailing the creation process below:
- Spring Container instantiates the bean
- It populates the bean properties
- If (bean instance of BeanNameAware)
- Container calls bean.setBeanName(bean id)
- If (bean instance of BeanFactoryAware)
- Container calls bean.setBeanFactory(container)
- If (bean instance of ApplicationContextFactoryAware)
- Container calls bean.setApplicationContext(container)
- If (container has BeanPostProcessors)
- Container calls postProcessBeforeInititialization() method of BeanPostProcessors
- If (bean instance of InitializingBean)
- Container calls bean.afterPropertiesSet()
- If bean declares custom init method
- Container calls custom init method of bean
- If (container has BeanPostProcessors)
- Container calls postProcessAfterInititialization() method of BeanPostProcessors
- Bean is now READY FOR USE
Similarly when the application is shutting down, it will result in the container being destroyed too. Naturally the beans are also approaching the end of their lifecycle. Before that there are a few steps:
- If (bean instance of DisposableBean)
- Container calls the bean.destroy()
- If bean declares custom destroy method
- Container calls custom destroy method of bean
- Bean is ready for garbage collection.
very good explanation
ReplyDeleteNice Algo .. Thanks for sharing
ReplyDeletethanks.
ReplyDeletegood algorithm.
super.......
ReplyDeleteAwesom explaination.
ReplyDeleteHi its a good explanation, you have missed BeanClassLoaderAware interface if you could add it will be complete information. Any how nice explanation
ReplyDeleteNice elaboration...
ReplyDelete