This is mainly asked in interviews so need to prepare properly concept and purpose of the same.
Spring framework is based on IOC so we call it as IOC container also So Spring beans reside inside the IOC container. Spring beans are nothing but Plain old java object (POJO).
Following steps explain their life cycle inside the container.
The container will look the bean definition inside configuration file (e.g. bean.xml).
Using reflection container will create the object and if any property is defined inside the bean definition then it will also be set.
If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called before the properties for the Bean are set.
If an init() method is specified for the bean, it will be called.
If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
If the Bean definition in the Configuration file contains a 'destroy-method' attribute, then the corresponding method definition in the Bean class will be called.
Try below program to understand flow:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
package com.javabykiran;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
public class Student implements BeanNameAware, BeanFactoryAware, DisposableBean {
Student() {
System.out.println("I am in constructor >> ");
}
Address address;
int age;
ArrayList<String> mobileNos;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
System.out.println("setter of address called");
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
System.out.println("setter of age called");
this.age = age;
}
public ArrayList<String> getMobileNos() {
return mobileNos;
}
public void setMobileNos(ArrayList<String> mobileNos) {
System.out.println("setter of mobile nos. called");
this.mobileNos = mobileNos;
}
public void setBeanName(String name) {
System.out.println("set bean name called .. name >> " + name);
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("set bean factory method called .. >> ");
System.out.println(beanFactory.getBean("address"));
}
public void destroy() throws Exception {
System.out.println("destro method from disposablebean called.");
}
public void init_method() {
System.out.println("init_method mentioned in xml called");
}
public void destroy_method() {
System.out.println("destroy_method mentioned in xml called");
}
@PostConstruct
public void initIt() throws Exception {
System.out.println("Init method after properties are set : ");
}
@PreDestroy
public void cleanUp() throws Exception {
System.out.println("Spring Clean Up! Employee is cleaned up now.");
}
}
<context:annotation-config />
<bean id="address" class="com.javabykiran.Address">
<property name="landmark" value="park plaza"></property>
</bean>
<bean id="stu" class="com.javabykiran.Student" scope="singleton"
init-method="init_method" destroy-method="destroy_method">
<property name="age" value="30"></property>
<property name="address">
<ref bean="address" />
</property>
<property name="mobileNos">
<list>
<value>8888809416</value>
<value>9552343698</value>
</list>
</property>
</bean>
// ConfigurableApplicationContext is subclass of
//ApplicationContext so there should not be confusion
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spconfig.xml");
Student student = (Student) context.getBean("stu");
context.close();