Pages Navigation Menu

Coding is much easier than you think

Using life cycle events on a bean

Using life cycle events on a bean

 


 
The life cycle events of a bean can be used to set values to the attributes of a bean using the init method.
We can associate the life cycle events of a bean to make calls to corresponding init and destroy method. The init and destroy method associated with a bean are given along with the bean definition. This way, a call is automatically made to these methods, as per the bean’s state.

Init method is called once the property of the bean is set.
Destroy method is called when the context associated with the bean is closed. This is when all the resources related to the context are cleared.

Step 1 :

Create a POJO with a method each for defining the steps to be taken at initial and destroy step. Note that the method name is user defined

File : Amount.java
 

package com.simpleCodeStuffs;

public class Amount {
	private String val;

	public String getVal() {
		return val;
	}

	public void setVal(String val) {
		this.val = val;
	}

	public String toString(){
		return ("amount bean val: "+val);
	}
	
	public void startUp(){
	     System.out.println("init() method called after properties to Amount are set. ");
	     System.out.println(toString());
	}
	
	public void cleanUp(){
	     System.out.println("destroy method called when application context is closed");
	     System.out.println("Releasing/destroying all resources");
	     System.out.println(toString());
	}
	}

 

Step 2 :

Bean definition is given along with the init and destroy values. The method names correspondingly are given here.

‘init-method’ and ‘destroy-method’ attributes are used along with the bean definition ‘bean’ tag, to specify the methods to be called during the start up and destruction stage of the bean

 
File : Beans.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-3.0.xsd">
	<bean id="amount" class="com.simpleCodeStuffs.Amount" init-method="startUp"
		destroy-method="cleanUp">
		<property name="val" value="SimpleCodeStuff--HelloWorld" />
	</bean>
</beans>

 

Step 3 : Create the main class

 
File : MainClass.java
 

package com.simpleCodeStuffs;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
	public static void main(String[] args) {
		
	ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "Beans.xml" });
		Amount amt = (Amount) context.getBean("amount");
		System.out.println("printing amount in main class " + amt);
		
	}
}

 

Step 4: Run it. The output appears as below

 

lifecycleOutput

Notice that, the destroy method has never been called. This is because the context is not closed.

 

Step 5 :

Make modifications in the main class to close the context. Note,we have initially used it as
 

  ApplicationContext context = 
	   new ClassPathXmlApplicationContext(new String[] {"Beans.xml"});
Amount amt=(Amount)context.getBean("amount");

 

But, close() method is not defined on ApplicationContext. The method is rather defined on the implementation ClassPathXmlApplicationContext. So let us modify that part as well  to implement our changes. Now the main class is
 

package com.simpleCodeStuffs;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainClass {
	public static void main(String[] args) {
	    ClassPathXmlApplicationContext context =
	   	 new ClassPathXmlApplicationContext(new String[] {"Beans.xml"}); 
                context.close()
//		ApplicationContext context = new ClassPathXmlApplicationContext(
//				new String[] { "Beans.xml" });
		Amount amt = (Amount) context.getBean("amount");
		System.out.println("printing amount in main class " + amt);
		context.close(); // destroy method is called here
	}
}

 

Step 6 : Run it. Now the output appears as :-

 

lifeCycleModifiedOutput

 


 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.