Spring Bean LifeCycle Details flow Diagram

Spring Bean LifeCycle Details flow Diagram

Spring Bean LifeCycle Details flow –

1) At First, spring creates an instance of the Bean using Java Reflection API.

2) If there are any properties associated with bean then spring injects those properties. If there are any bean as a peoperty then spring finds that bean and inject it.

3) If the Bean class implements the BeanNameAware interface, then the spring will call setBeanName()method by passing the name of the Bean.

4) If the Bean class implements the BeanFactoryAware interface, then the spring will callsetBeanFactory() method by passing an instance of BeanFactory object.

Spring-Bean-LifeCycle

5) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessBeforeInitialization() method before the properties for the Bean are injected.

6) If the Bean class implements the InitializingBean interface, then the spring will callafterPropertiesSet() method once all the Bean properties defined in the Configuration file are injected.

7) If there is any custom init-method declared in the configuration file, that method will be called.

8) If there are any BeanPostProcessors associated with the BeanFactory that loads the Bean, then the spring will call postProcessAfterInitialization() method.

9) Now bean is ready for use.

Spring Frameworks Bean LifeCycle callbacks –

Spring Framework provides InitializingBean and DisposableBean marker interface. Implementing these interfaces will result in the container calling afterPropertiesSet() for the initialization and destroy() for the destruction to allow the bean to perform certain actions upon initialization and destruction.

Initialization callbacks:

The org.springframework.beans.factory.InitializingBean interface defines a single method:

void afterPropertiesSet() throws Exception;

So you can implement InitializingBean interface and initialization work can be done inside afterPropertiesSet() method as follows:

Or you can use custom init-method attribute in spring xml configuration file to specify the name of the method. For example:

<bean id=”myInitBean”
class=”org.gopaldas.MyInitBean” init-method=”init”/>

Following is the class definition:

public class MyInitBean {
public void init() {
// do initialization work
}
}

Destruction callbacks:

The org.springframework.beans.factory.DisposableBean interface defines a single method:

void destroy() throws Exception;

So you can implement above interface and finalization work can be done inside destroy() method as follows:

public class MyDestroyBean implements DisposableBean {
public void destroy() {
// do destruction work
}
}

Or you can use custom destroy-method attribute in spring xml configuration file to specify the name of the method. For example:

<bean id=”myDestroyBean”
class=”org.gopaldas.MyDestroyBean” destroy-method=”destroy”/>

Following is the class definition:

public class MyDestroyBean implements DisposableBean {
public void destroy() {
// do destruction work
}
}

@PostConstruct and @PreDestroy annotations

The easiest way to control a bean’s lifecycle is by using the two lifecycle annotations above custom methods that will be invoked upon bean initialization and destruction.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *