LifeCycle callback in spring – Spring provides call back methods for the life cycle of the beans. We can have a method in our bean that gets run when the bean is created and we can have a method which will run when the bean is about destroy. Hence we can use this methods for some initialization activity and for clean up activity as well .
Lets see how the lifecycle call back works in Spring with the below example code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.j2eereference.spring; public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void display(){ System.out.println("Name is "+name); System.out.println("Age is "+age); } public void myInit(){ System.out.println("This is from myInit Method"); } public void myDestroy(){ System.out.println("This is from myDestroy Method"); } } |
Spring.xml
1 2 3 4 5 6 7 8 9 10 |
<?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.xsd"> <bean id="personBean" class="com.j2eereference.spring.Person" init-method="myInit" destroy-method="myDestroy"> <property name="name" value="Leo"/> <property name="age" value="30"/> </bean> </beans> |
Now lets test this configuration with the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.j2eereference.spring; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { public static void main(String[] args) { AbstractApplicationContext context= new ClassPathXmlApplicationContext("spring.xml"); context.registerShutdownHook(); Person person= (Person)context.getBean("personBean"); person.display(); } } |
context.registerShutdownHook(); – This is required only in case of desktop application, the ShutdownHook is called when main method exit.
If you have a web application you don’t have to do this, rather Spring context will know when to end.
Output:
This is from myInit Method
Name is Leo
Age is 30
This is from myDestroy Method
Here you can notice that the myInit method is getting called before executing the display method and myDestroy method is getting called at the end.
If you have scenario where in you want to call the same init and destroy method for all the defined beans . We can set it as default-init-method and default-destroy-method as below.
1 2 3 4 5 6 7 8 9 10 11 |
<?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.xsd" <strong>default-init-method</strong>="myInit" <strong>default-destroy-method</strong>="myDestroy"> <bean id="personBean" class="com.j2eereference.spring.Person"> <property name="name" value="Leo"/> <property name="age" value="30"/> </bean> </beans> |
[…] know how to configure lifecycle callbacks using configuration xml file. Here lets find out how to configure the same without using the configuration xml […]