A bean’s life
(Spring Bean Life Cycle):
- Spring instantiates the bean.
- Spring injects values and
bean references into the bean’s properties.
- If the bean implements BeanNameAware, Spring passes the bean’s ID to
the set-BeanName()
method.
- If the bean implements BeanFactoryAware, Spring calls the setBeanFactory() method, passing in the bean
factory itself.
- If the bean implements ApplicationContextAware, Spring will call the
set-ApplicationContext()
method, passing in a reference to the enclosing application context.
- If any of the beans implement
the BeanPostProcessor interface, Spring
calls their postProcessBeforeInitialization()
method.
- If any beans implement the InitializingBean interface, Spring calls
their afterPropertiesSet() method.
Similarly, if the bean was declared with an init-method,
then the specified initialization method will be called.
- If there are any beans that
implement BeanPostProcessor, Spring
will call their postProcessAfterInitialization()
method.
- At this point, the bean is
ready to be used by the application and will remain in the application
context until the application context is destroyed.
- If any beans implement the DisposableBean interface, then Spring will call
their destroy() methods. Likewise, if
any bean was declared with a destroy-method,
then the specified method will be called.
Ref: Spring in Action By Craig Walls