Spring Bean Life Cycle by R4R Team

Spring Bean Life Cycle:-
The most important feature of Spring is the bean based approach. The Spring bean is created, managed and dispensed by the Spring IoC container. Each Spring bean has a lifecycle and understanding the spring bean lifecycle enables better coding.
The life cycle of a Spring bean is very easy to understand. When a bean is instantiated, it may be required to perform some initialization to get it into a usable state.The following are the stages in a beanís   lifecycle.

Archetecture of Spring Bean Life Cycle:

1 Instantiate - The Spring container instantiates the bean.

2 Populate properties- Spring IoC container injects the beanís properties.

3 Set Bean Name- Spring container sets the bean name. If the bean implements BeanNameAware, spring container passes the beanís id to setBeanName() method.

4 Set Bean Factory-If the bean implements BeanFactoryAware, Spring container passes theBeanFactory to setBeanFactory().

5 Pre Initialization-This stage is also called the bean postprocess . If there are anyBeanPostProcessors, theSpring container calls the postProcesserBeforeInitialization () method.

6 Initialize beans- If the bean implements IntializingBean,its afterPropertySet()method is called. If the bean has init method declaration, the specified initialization method is called.

7 Post Initialization- IfBeanPostProcessors is implemented by the bean, the Spring container calls their postProcessAfterinitalization() method.

8 Ready to Use- Now the bean is ready to be used by the application.

9 Destroy- The bean is destroyed during this stage. If the bean implements DisposableBean, the Spring IoC container will call the destroy() method . If a custom destroy () method is defined, the container calls the specified method.

InitializingBean and DisposableBean callback interfaces:-
Initialization callbacks:
The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container. The InitializingBean interface specifies a single method:
1 Single method
void afterPropertiesSet() throws Exception;

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

2 afterPropertiesSet() method

public class Bean implements InitializingBean {
public void afterPropertiesSet() {
// perform some initialization work
} }

When using the XML-based configuration metadata, you can use the init-method attribute to specify the name of the method that has a return void, no argument method signature. The following is example.

3 Example
<bean id="Bean"
class="com.r4r.Test.Bean" init-method="init"/>

4 The Test class definition:
public class Bean {
public void init() {
// do some initialization work
} }

Destruction callbacks:
The org.springframework.beans.factory. DisposableBean interface specifies a single method:
5 Single method
void destroy() throws Exception;
So to enable a destroy callback, simply implement the above interface and finalization work can be done inside destroy() method as follows:

6 destroy()

public class Bean implements DisposableBean {
public void destroy() {
// do the work to be performed before bean destruction
} }

When using the XML-based configuration metadata, you can use the destroy-method attribute to specify the name of the method that has a return void, no argument method signature. The following is example.

7 example
<bean id="sampleBean"
class="com.r4r.Test.Bean" destroy-method="destroy"/>

8 The class definition
public class Test {
public void destroy() {
// do some destruction work
} } 

Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!