Life Cycle of Bean Factory and Application Context
Life Cycle of Bean Factory
In Spring, Bean factory is used to instantiate, configure and manage objects. The objects created by Bean factory are aware of the relationships with other objects. Bean factory also handles the life cycle of objects.
Bean factory populates all the properties from XML file.
Set Bean Name:
Name of the bean is set, This is done by passing the bean id in XML file to setBeanName() method (from BeanNameAware Interface)
Set Bean Factory:
In this step, Bean is given a reference to the bean factory. This is done by method setBeanFactory() from BeanFactoryAware interface.
This performs all the tasks that needs to be performed before the been is initialized. This is done by postProcessBeforeInitialization() method from BeanPostProcessor interface.
Performs initializations before bean is ready to be used. Like opening database connections etc.
Performs processes that needs to be performed after been is initialized. Method used postProcessAfterInitialization() from BeanPostProcessor interface.
Bean Ready to Use:
Bean is ready to use by the application.
Destroy the Bean:
Bean is destroyed. Method used is destroy() from DisposableBean interface.
Life Cycle of Application Context:
Application context is built over Bean Factory, so application context will inherit all the capabilities of BeanFactory. In addition to that, Application context provides,
- Support Internalization
- Events to beans, which are registered Listeners
Steps are same as Bean Factory Life Cycle, But it has one Step between SetbeanFactory and Pre-Initialization – SetApplicationContext
This step is only valid if application Context is used. Method used is setApplicationContext() from ApplicationContextAware Interface.