Spring Bean Life Cycle

  • Facebook
  • Google
  • LinkedIn
  • Twitter

IOC Container manages the Spring bean Life Cycle that is where spring people came up with approach Bean Life Cycle. Now we can write the initialization logic.

Here are the points which container performs at the time of bean initialization

  • Container will look the bean definition inside configuration file .(Xml file) in our example applicationContext.xml file
  • Spring container instantiates the bean using Java Reflection API.
  • Spring container applies the all specified properties using Dependency Injection.
  • If the bean class implements the BeanNameAware interface, then spring container calls the setBeanName() method by passing bean`s id.
  • If the bean class implements the BeanClassLoaderAware interface, then spring container calls the setBeanClassLoader() method by passing an instance of the ClassLoader object that loaded this bean.
  • If the bean class implements the BeanFactoryAware interface, then spring container calls setBeanFactory() method by passing an instance of BeanFactory object.
  • If there are any BeanPostProcessors object associated with the BeanFactory than spring container calls their postProcessBeforeInitialization() method even before setting the properties for the bean.
  • If the bean class implements the InitializingBean interface, then spring container calls the afterPropertiesSet() method after setting bean properties.
  • If init-method is specified in configuration file for the bean then spring container calls the corresponding method in the bean class.
  • If there are any BeanPostProcessors associated with the bean then spring container calls the postProcessAfterInitialization() method.
  • Container is going to shut down.if a bean implements disposable interface then its destroy method will be called.

Spring Bean Life Cycle Declarative approach Example

Fruits.java

package com.javatutsworld;
public class Fruits {
	private String fruitsName;
	private int qty;
	
	public void myInit() {
	 System.out.println("Initializing Fruits ");
	}

	public void myDestroy() {
	 qty=0;
	 //releasing Fruits;
	}
	
	public String getFruitsName() {
		return fruitsName;
	}
	public void setFruitsName(String fruitsName) {
		this.fruitsName = fruitsName;
	}
	public int getQty() {
		return qty;
	}
	public void setQty(int qty) {
		this.qty = qty;
	}	
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	
<bean id="fruitsObj" class="com.javatutsworld.Fruits" init-method="myInit" destroy-method="myDestroy">
	<property name="fruitsName" value="Apple"/>
	<property name="qty" value="10"/>
 </bean>	
</beans> 

MainApp.java

package com.javatutsworld;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class MainApp {

 public static void main(String[] args)
 {		
	Resource res = new ClassPathResource("applicationContext.xml");
	BeanFactory beans=new XmlBeanFactory(res);		
	Fruits fr=(Fruits)beans.getBean("fruitsObj");
	
	System.out.println("Fruits Name: "+fr.getFruitsName());
	System.out.println("Fruits Quantity: "+fr.getQty()+"kg");		
 }
} 

Output

Oct 30, 2015 5:38:41 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Initializing Fruits 
Fruits Name: Apple
Fruits Quantity: 10kg
Spring Bean Life Cycle Programmatic approach Example

Fruits.java

package com.javatutsworld;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Fruits implements InitializingBean, DisposableBean {
	private String fruitsName;
	private int qty;

	public void afterPropertiesSet() {
		System.out.println("Initializing Fruits ");
	}

	public void destroy() {
		qty = 0;
		// releasing Fruits;
	}

	public String getFruitsName() {
		return fruitsName;
	}

	public void setFruitsName(String fruitsName) {
		this.fruitsName = fruitsName;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/tx 
    	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	
 <bean id="fruitsObj" class="com.javatutsworld.Fruits">
	<property name="fruitsName" value="Apple"/>
	<property name="qty" value="10"/>
 </bean>	
</beans>

MainApp.java

package com.javatutsworld;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class MainApp {
 public static void main(String[] args)
 {		
	Resource res = new ClassPathResource("applicationContext.xml");
	BeanFactory beans=new XmlBeanFactory(res);		
	Fruits fr=(Fruits)beans.getBean("fruitsObj");
	System.out.println("Fruits Name: "+fr.getFruitsName());
	System.out.println("Fruits Quantity: "+fr.getQty()+"kg");		
 }
}

Output

Oct 30, 2015 5:50:36 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Initializing Fruits 
Fruits Name: Apple
Fruits Quantity: 10kg
      Advertisements