Spring Framework : A Framework from SpringSource :: Spring Bean Life Cycles
- What is Bean Life Cycle?
- Bean Life Cycle through init-method and destroy attributes of the <bean> tag
- Bean Life Cycle through InitializingBean and DisposableBean Interfaces
- Download Examples
What is Bean Life Cycle? |
- The Spring Framework provides several marker interfaces to change the behavior of your bean in the container; they include InitializingBean and DisposableBean.
- Implementing these interfaces will result in the container calling afterPropertiesSet() for the former and destroy() for the latter to allow the bean to perform certain actions upon initialization and destruction.
- Internally, the Spring Framework uses BeanPostProcessor implementations to process any marker interfaces it can find and call the appropriate methods. If you need custom features or other lifecycle behavior Spring doesn’t offer out-of-the-box, you can implement a BeanPostProcessor yourself.
- All the different lifecycle marker interfaces are described below. In one of the appendices, you can find diagram that show how Spring manages beans and how those lifecycle features change the nature of your beans and how they are managed.
- Developing our own methods for initialization and destroy operations in POJO class and then give an instruction to IOC container by using init-method and destroy attributes of the <bean> tag. Pass the initialization and destroy method names of the POJO class as the values to init-method and destroy attributes respectively.
- Implementing InitializationBean and DisposableBean interface in order to perform initialization and destroy operations. After implementing the interface override the afterPropertiesSet() and destroy() method respectively for initialization and destroy operations.
Bean Life using Attributes Examples |
Examples Using Own Methods in Bean and Using bean tag with init-method and destroy-method attributes
Item.Java
package com.javaskool;
public class Item {
private int itemId;
private String itemName;
private int qty;
public void myInitialize() {
itemId=5001;
System.out.println("Initializing Item ");
}
public void myDestroy() {
itemId=0;
qty=0;
//releasing Item;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="obj" class="com.javaskool.Item" init-method="myInitialize" destroy-method="myDestroy">
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
</beans>
TestDrive.java
package com.javaskool;
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 TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Item p1=(Item)beans.getBean("obj"); //getting Object from BeanFactory
System.out.println(p1.getItemId());
System.out.println(p1.getItemName());
System.out.println(p1.getQty());
}
}
Output
Initializing Item
5001
Mouse
500
Bean Life using Interfaces Examples |
Examples Using Existing Methods in Bean by implementing InitializationBean and DisposableBean interface.
Item.Java
package com.javaskool;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Item implements InitializingBean, DisposableBean {
private int itemId;
private String itemName;
private int qty;
public void afterPropertiesSet() {
itemId=5001;
System.out.println("Initializing Item ");
}
public void destroy() {
itemId=0;
qty=0;
//releasing Item;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="obj" class="com.javaskool.Item">
<property name="itemName" value="Mouse"/>
<property name="qty" value="500"/>
</bean>
</beans>
TestDrive.java
package com.javaskool;
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 TestDrive {
public static void main(String[] args)
{
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory beans=new XmlBeanFactory(res);
Item p1=(Item)beans.getBean("obj"); //getting first Object from BeanFactory
System.out.println(p1.getItemId());//value from initialization
System.out.println(p1.getItemName());
System.out.println(p1.getQty());
}
}
Output
Initializing Item
5001
Mouse
500
Click Below to download the Examples
Recent Comments