Lifecycle of a Bean is managed by the Spring Framework (IoC container).
In spring bean instantiation to destruction, there is a series of steps has been performed by Spring container. Each stage has its own predefined task. One thing which is really good about Spring that is we can hook to every stage of Spring through callback methods.
Different stages of Spring bean

Instantiation
Spring container instantiate Spring Bean.
Populate Properties
Spring container set the properties of the bean by injection (from Spring configuration XML).
SetBeanName
Set the identifier of the bean.
SetBeanFactory
If BeanFactoryAware has been implemented, Container passes the BeanFactory context into the bean.
SetApplicationContext
If ApplicationContextAware has been implemented, Container passes the Applicationcontext into the bean.
PreIntialization
If BeanPostPeocessor interface has been implemented in a bean container, spring calls bean’s postProcessBeforeIntialization method for doing any preprocess stuff.
afterPropertySet
If an old intializingBean interface has been implemented in a bean, it calls the afterPropertySet method of that interface to do any initialization stuff after bean property has been set.
init()
Call init method for any initialization stuff.
PostIntializaton
If BeanPostPeocessor interface has been implemented in a bean container, Spring calls bean’s postProcessAfterIntialization method for doing any post processing stuff.
Destroy
If old disposableBean interface has been implemented by a bean, spring calls the destroy method of that interface to do any pre-destroy stuff before bean deletion from the container.
Custom Destroy
call destroy method for any pre-destroy stuff.
Spring provides 2 life cycles methods to every Bean that it creates and manages.
They are :
- public void init( ) method and
- public void destroy( ) method.
Name of the methods need not be same, but the signatures should be exactly same.
Any initialization code like, loading some configuration file from Fileystem, or connecting to DB/Service/Server should go in init( ) method. Clean-up code like close the connections, etc will go into destroy( ) method.
- IoC container first creates the objects/instantiates the objects
- Then, sets the values on to the fields. All dependencies are populated
- init( ) method called
- Bean is available for usage.
- destroy( ) method called
- Bean is no longer available.
How do you configure init and destroy methods :
- Using XML configuration
- Using Spring interfaces
- Using annotations – @PostConstruct and @PreDestroy
XML Configuration :
class Employee {
....
public void hi() {
System.out.println("Hello");
}
public void bye() {
System.out.println("Bye");
}
....
}
<bean name="emp" class="org.rdayala.spring.basics.Employee"
p:empId="10" p:empName="Raghu"
init-method="hi"
destroy-method="bye" />
In the XML configuration, even though we configured destroy-method, it is not called. For this, we need to register pre shutdown hook on to the container for the destroy method to be called. This shutdown hook is not available in the ApplicationContext class. We need to use, AbstractApplicationContext class.
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("config.xml");
.....
context.registerShutdownHook(); -> tells Spring IoC to invoke destroy methods.
Using Spring Interfaces :
For this, POJO Bean class has to implement :
- InitializingBean interface for init method and
- DisposableBean interface for destroy method.
class ABC implements InitializingBean, DisposableBean {
.....
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("init method called");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy method called");
}
....
}
We still need to register pre shutdown hook on to the container for the destroy method to be called. This shutdown hook is not available in the ApplicationContext class. We need to use, AbstractApplicationContext class. [Check above XML Configuration.]
Using Annotations
By default, Spring will not aware of the @PostConstruct and @PreDestroy annotations. To enable it, you have to specify the ‘<context:annotation-config />‘ in bean configuration file.
Note
The @PostConstruct and @PreDestroy annotation are not belong to Spring, it’s located in the J2EE library – common-annotations.jar.
- @PostContruct – This annotation is applied to a method to indicate that it should be invoked after bean is created and all dependency injection is complete.
- @PreDestroy – This is applied to a method to indicate that it should be invoked before the bean is removed from the Spring context, i.e. just before it’s destroyed.
@PostConstruct
public void hi() {
System.out.println("Post construct method called");
}
@PreDestroy
public void bye() {
System.out.println("Pre destroy method called");
}
We still need to register pre shutdown hook on to the container for the destroy method to be called. This shutdown hook is not available in the ApplicationContext class. We need to use, AbstractApplicationContext class. [Check above XML Configuration.]