What is Spring Bean life cycle?
Spring Beans are initialized by Spring IOC Container and all the dependencies are also injected. When context is destroyed, it also destroys all the initialized beans. This works well in most of the cases but sometimes we want to initialize other resources or do some validation before making our beans ready to use. To do this, Spring framework provides support for post-initialization and pre-destroy methods in spring beans.
We can do this by two ways:
Spring Beans are initialized by Spring IOC Container and all the dependencies are also injected. When context is destroyed, it also destroys all the initialized beans. This works well in most of the cases but sometimes we want to initialize other resources or do some validation before making our beans ready to use. To do this, Spring framework provides support for post-initialization and pre-destroy methods in spring beans.
We can do this by two ways:
- By implementing InitializingBean and DisposableBean interfaces
- By using init-method and destroy-method attribute in spring bean configurations.