In this tutorial we are going to see about Spring Life cycle Destruction callbacks with example program.
Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies a single method:
It is recommended that you do not use the DisposableBean callback interface because it unnecessarily couples the code to Spring. Alternatively, use the @PreDestroy annotation or specify a generic method that is supported by bean definitions. With XML-based configuration metadata, you use the destroy-method attribute on the <bean/>. For example, the following definition:
...is exactly the same as...
... but does not couple the code to Spring. We will see both types with example.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringDisposableBean.
3. Add the following jars into your build path.
5. Next create SpringDestructionCallback class under com.javatutorialscorner.spring package . This class using cleanup() method to destroy bean.
SpringDestructionCallback.java
5. Next create SpringDestructionCallback1 class under com.javatutorialscorner.spring package . This class implements DisposableBean and using default destroy() method to destroy bean.
SpringDestructionCallback1 .java
7. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
In above program
First step I created Application context using ClassPathXmlApplicationContext which load configuration from bean file which located in class path of apllication, It take care of creating and initializing all the objects (Beans) declared in bean.xml
Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.
Once Object Created you can access any method from that class.
The registerShutdownHook() method declared in AbstractApplicationContext class, which ensure the graceful shutdown and class relevant destroy method.
7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).
Bean.xml
Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.
Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to variable sayHello without changing the class file.
Now you can run the program see the following output in console.
Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies a single method:
1.
void
destroy()
throws
Exception;
It is recommended that you do not use the DisposableBean callback interface because it unnecessarily couples the code to Spring. Alternatively, use the @PreDestroy annotation or specify a generic method that is supported by bean definitions. With XML-based configuration metadata, you use the destroy-method attribute on the <bean/>. For example, the following definition:
1.
<
bean
id
=
"cleanupBean"
class
=
"com.javatutorialscorner.spring.SpringDestructionCallback"
destroy-method
=
"cleanup"
/>
1.
public
class
SpringDestructionCallback{
2.
3.
public
void
cleanup() {
4.
// do some destruction work (like releasing pooled connections)
5.
}
6.
}
...is exactly the same as...
1.
<
bean
id
=
"destroyBean"
class
=
"com.javatutorialscorner.spring.SpringDestructionCallback1"
/>
1.
public
class
SpringDestructionCallback1
implements
DisposableBean {
2.
3.
public
void
destroy() {
4.
// do some destruction work (like releasing pooled connections)
5.
}
6.
}
... but does not couple the code to Spring. We will see both types with example.
Step by step procedure to create Spring Program using Eclipse given below.
Follow the Simple steps.
1. Select File –> New –> Java Project from your Eclipse IDE.
2.Create project called SpringDisposableBean.
3. Add the following jars into your build path.
4. Now create package com.javatutorialscorner.spring under SpringDisposableBean Project
commons-logging-1.1.3.jar
spring-beans-3.2.6.RELEASE.jar
spring-context-3.2.6.RELEASE.jar
spring-context-support-3.2.6.RELEASE.jar
spring-core-3.2.6.RELEASE.jar
spring-expression-3.2.6.RELEASE.jar
5. Next create SpringDestructionCallback class under com.javatutorialscorner.spring package . This class using cleanup() method to destroy bean.
SpringDestructionCallback.java
01.
package
com.javatutorialscorner.spring;
02.
03.
public
class
SpringDestructionCallback {
04.
private
String sayHello;
05.
06.
public
void
getSayHello() {
07.
System.out.println(
"Java Tutorials Corner "
+ sayHello);
08.
}
09.
10.
public
void
setSayHello(String sayHello) {
11.
this
.sayHello = sayHello;
12.
}
13.
14.
public
void
cleanup()
throws
Exception {
15.
// TODO Auto-generated method stub
16.
System.out
17.
.println(
"Inside SpringDestructionCallback - cleanup() method - bean will destroy now"
);
18.
}
19.
}
5. Next create SpringDestructionCallback1 class under com.javatutorialscorner.spring package . This class implements DisposableBean and using default destroy() method to destroy bean.
SpringDestructionCallback1 .java
01.
package
com.javatutorialscorner.spring;
02.
03.
import
org.springframework.beans.factory.DisposableBean;
04.
05.
public
class
SpringDestructionCallback1
implements
DisposableBean {
06.
07.
private
String sayHello;
08.
09.
public
void
getSayHello() {
10.
System.out.println(
"Java Tutorials Corner "
+ sayHello);
11.
}
12.
13.
public
void
setSayHello(String sayHello) {
14.
this
.sayHello = sayHello;
15.
}
16.
17.
@Override
18.
public
void
destroy()
throws
Exception {
19.
// TODO Auto-generated method stub
20.
System.out.println(
"Inside SpringDestructionCallback1 - destroy() method - bean will destroy now"
);
21.
22.
}
23.
}
7. Now Create RunApp class which contains Bean configuration path to load configuration from Bean.xml file
RunApp.java
01.
package
com.javatutorialscorner.spring;
02.
03.
import
org.springframework.context.support.AbstractApplicationContext;
04.
import
org.springframework.context.support.ClassPathXmlApplicationContext;
05.
06.
public
class
RunApp {
07.
08.
/**
09.
* @param args
10.
*/
11.
public
static
void
main(String[] args) {
12.
// TODO Auto-generated method stub
13.
14.
AbstractApplicationContext context =
new
ClassPathXmlApplicationContext(
15.
"Beans.xml"
);
16.
SpringDestructionCallback destructionBean = (SpringDestructionCallback) context
17.
.getBean(
"cleanupBean"
);
18.
destructionBean.getSayHello();
19.
20.
SpringDestructionCallback1 destructionBean1 = (SpringDestructionCallback1) context
21.
.getBean(
"destroyBean"
);
22.
destructionBean1.getSayHello();
23.
24.
context.registerShutdownHook();
25.
}
26.
27.
}
In above program
First step I created Application context using ClassPathXmlApplicationContext which load configuration from bean file which located in class path of apllication, It take care of creating and initializing all the objects (Beans) declared in bean.xml
Next getBean() method used to get particular bean from created context. This method used bean name as parameter and returns generic object. We can caste to actual object.
Once Object Created you can access any method from that class.
The registerShutdownHook() method declared in AbstractApplicationContext class, which ensure the graceful shutdown and class relevant destroy method.
7. Create Bean.xml at where your class files created (ClassPathXmlApplicationContext tries to load bean file from class path).
Bean.xml
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03.
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04.
xsi:schemaLocation
=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
05.
06.
<
bean
id
=
"cleanupBean"
class
=
"com.javatutorialscorner.spring.SpringDestructionCallback"
destroy-method
=
"cleanup"
>
07.
<
property
name
=
"sayHello"
value
=
"cleanupBean"
></
property
>
08.
</
bean
>
09.
<
bean
id
=
"destroyBean"
class
=
"com.javatutorialscorner.spring.SpringDestructionCallback1"
>
10.
<
property
name
=
"sayHello"
value
=
"destroyBean"
></
property
>
11.
</
bean
>
12.
</
beans
>
Bean.xml is default name given to bean configuration file. You can choose any name for your bean.xml but you can use file name in main application to create context and your file available in your class path must be same.
Bean.xml is used to assign unique ID to different beans and controls the creation object with different values, using above file you can pass any value to variable sayHello without changing the class file.
Now you can run the program see the following output in console.
Java Tutorials Corner cleanupBean
Java Tutorials Corner destroyBean
Inside SpringDestructionCallback1 - destroy() method - bean will destroy now
Inside SpringDestructionCallback - cleanup() method - bean will destroy now
0 comments:
Post a Comment