Uncategorized

BEAN INITIALIZATION AND DESTRUCTION IN SPRING

Spring container provides options to call methods before bean construction and after bean destruction using which you can do the pre-processing and post-processing tasks.To do that you need to add an attribute init-method=”name of method” to call the method before bean initialization and attribute destroy-method=”name of method” to call the method after bean destruction in your bean definition in the XML file.


<bean id="name of id" class="name of class" init-method="name of initialization method" destroy-method="name of destruction method" />

Example :

Let’s implement the same example from our previous tutorial and include these attributes.

Phone.java


package com.codegeekslab.device;

public interface Phone {

public void openApp(int number);

}

BasicPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class BasicPhone implements Phone {

public void openApp(int number) {
System.out.println("calling via simcard... " + number);
}

public void checkNetwork() {
System.out.println("network found");
}

public void printTime() {
System.out.println("you talked for 10 min");
}
}

SmartPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class BasicPhone implements Phone {

public void openApp(int number) {
System.out.println("calling via simcard... " + number);
}

public void checkDuration() {
System.out.println("network found");
}

public void printTime() {
System.out.println("you talked for 10 min");
}
}

CallingApp.java


package com.codegeekslab.app;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

// for setter injection
public Phone getPhone() {
return phone;
}

public void setPhone(Phone phone) {
this.phone = phone;
}

public void makeCall(int number) {
phone.openApp(number);
}

}

beans.xml


<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.5.xsd">

<bean id="BasicPhone" class="com.codegeekslab.type.BasicPhone"
init-method="checkNetwork" destroy-method="checkDuration" />
<bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone" />
<bean id="CallingApp" class="com.codegeekslab.app.CallingApp">
<property name="phone" ref="BasicPhone" />
</bean>

</beans>

Test.java


package com.codegeekslab.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.codegeekslab.app.CallingApp;

public class Test {

public static void main(String[] args) {
ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
CallingApp callingApp = (CallingApp) context.getBean("CallingApp");
callingApp.makeCall(777);
((GenericXmlApplicationContext) context).close();
}
}

output


Mar 31, 2017 11:15:27 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 31, 2017 11:15:27 AM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Fri Mar 31 11:15:27 IST 2017]; root of context hierarchy
network found
calling via simcard... 777
Mar 31, 2017 11:15:27 AM org.springframework.context.support.GenericXmlApplicationContext doClose
INFO: Closing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Fri Mar 31 11:15:27 IST 2017]; root of context hierarchy
you talked for 10 min

As you can see i have i have added init-method and destroy-method attributes in the beans.xml and checkNetwork and printDuration methods respectivley in BasicPhone class .So before the spring container creates the bean of BasicPhone class it’s going to call checkNetwork method and it’s going to call checkDuration after we close the ApplicationContext using ((GenericXmlApplicationContext) context).close();

 

Leave a Reply

Your email address will not be published. Required fields are marked *