Showing posts with label Spring Lifecycle Callbacks. Show all posts
Showing posts with label Spring Lifecycle Callbacks. Show all posts

March 10, 2017

#Spring part 3 : Lifecycle Callbacks

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:
  • By implementing InitializingBean and DisposableBean interfaces 
  • By using init-method and destroy-method attribute in spring bean configurations.
registerShutdownHook(): To close the application context, we need to use AbstractApplicationContext which registers the registerShutdownHook() for the Java programme. This will ensure a graceful shutdown and call the relevant destroy methods. It is used for desktop applications, not for the web applications.