Spring bean life cycle call back Methods Example

Spring framework provides bean life cycle call back methods to perform some additional tasks which you may want to perform when a bean is initiated or created or when a bean is about to get destroyed.

Therefore you can run a method which will do some initialization process during bean initialization and you can run another method which will do some clean up process just before a bean gets destroyed.

Spring container is responsible for managing the life cycle of beans. The call back methods of bean life cycle can be categorized in two groups:

  1. post-initialization call back methods
  2. pre-destruction call back methods.

Spring framework provides four ways for controlling life cycle events of beans:

  1. InitializingBean and DisposableBean interfaces
  2. Spring *Aware interfaces for specific behavior
  3. Custom init() and destroy() methods in bean configuration
  4. Using @PostConstruct and @PreDestroy annotations

InitializingBean and DisposableBean

The InitializingBean interface is often used along with the FactoryBean interface. It helps to initialize the factory before it produces an object. The InitializingBean interface specifies a single method void afterPropertiesSet() throws Exception;. InitializingBean is a marker interface.

This is not a preferable way to initialize the bean because it will tightly couple your bean with spring container. A better approach is to use “init-method” in XML file or “initMethod” in Java based configuration for bean definition.

Similarly, implementing the DisposableBean interface allows a bean to get a call back when the container is going to destroy it. The DisposableBean interface specifies a single method void destroy() throws Exception;. So this is also a marker interface.

For this also you can use “destroy-method” in XML file or “destroyMethod” in Java based configuration for bean definition.

Read example on InitializingBean and DisposableBean call back interfaces in Spring.

Spring *Aware Interfaces

There are many *Aware interfaces (for example, ApplicationContextAware, BeanFactoryAware, etc.) provided by Spring framework and these interfaces allow you to look into the inner workings of the Spring Framework. These interfaces allow beans to indicate to the container that they require a certain infrastructure dependency.

You can read an example on *Aware interfaces in Servlet application.

Custom init() and destroy() Methods

You can define default init() and destroy() methods for bean configuration in two ways:

  1. Per bean or local configuration
  2. For all beans or global configuration

You can read example on custom init() and destroy() methods in Spring.

@PostConstruct and @PreDestroy Annotations

@PostConstruct annotated method will be invoked after the bean has been constructed using default constructor and just before its instance is returned to the requesting object.

@PreDestroy annotated method is called just before the bean is about to be destroyed by the Spring container.

You can read an example on @PostConstruct and @PreDestroy annotations.

Thanks for reading.

Leave a Comment