Spring provides call back methods for initialization and
clean up your code.
You can do it in two ways
1.
By implementing Initializing and DisposableBean
interfaces
2.
By configuring bean in xml using init-method and destroy-method
Before implementing call back methods you need to import AbstractApplicationContext in your main
methods and you need to call registerShutdownHook()
method, if you don’t add this method then destroy method will not be called.
By implementing Initializing
and DisposableBean interfaces
DrawingApp.java
package org.yash.watertechsol;
import org.springframework.context.support.AbstractApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[]
args){
AbstractApplicationContext
context = new
ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();
Triangle triangle
= (Triangle)context.getBean("triangle");
triangle.draw();
}
}
Triangle.java
package org.yash.watertechsol;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Triangle implements InitializingBean, DisposableBean{
private Point pointA;
private Point pointB;
private Point pointC;
public Point
getPointA() {
return pointA;
}
public void setPointA(Point
pointA) {
this.pointA = pointA;
}
public Point
getPointB() {
return pointB;
}
public void setPointB(Point
pointB) {
this.pointB = pointB;
}
public Point
getPointC() {
return pointC;
}
public void setPointC(Point
pointC) {
this.pointC = pointC;
}
public void draw(){
System.out.println("Point A
= (" + getPointA().getX()+", "+getPointA().getY()+")");
System.out.println("Point B
= (" + getPointB().getX()+", "+getPointB().getY()+")");
System.out.println("Point C
= (" + getPointC().getX()+", "+getPointC().getY()+")");
}
@Override
public void destroy() throws Exception {
System.out.println("CleanUp
Code will be called here");
}
@Override
public void
afterPropertiesSet() throws Exception {
System.out.println("Initialization
code will be called here");
}
}
Point.java
package org.yash.watertechsol;
public class Point {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE
beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle"
class="org.yash.watertechsol.Triangle" autowire="byName">
</bean>
<bean id
="pointA"
class="org.yash.watertechsol.Point">
<property name="x"
value="0" />
<property name="y"
value="0" />
</bean>
<bean id
="pointB"
class="org.yash.watertechsol.Point">
<property name="x"
value="-20" />
<property name="y"
value="0" />
</bean>
<bean id
="pointC"
class="org.yash.watertechsol.Point">
<property name="x"
value="0" />
<property name="y"
value="20" />
</bean>
</beans>
By configuring bean in xml using init-method and destroy-method
spring.xml
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE
beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle"
class="org.yash.watertechsol.Triangle" autowire="byName" init-method="myInit"
destroy-method="cleanUp">
</bean>
<bean id
="pointA"
class="org.yash.watertechsol.Point">
<property name="x"
value="0" />
<property name="y"
value="0" />
</bean>
<bean id
="pointB"
class="org.yash.watertechsol.Point">
<property name="x"
value="-20" />
<property name="y"
value="0" />
</bean>
<bean id
="pointC"
class="org.yash.watertechsol.Point">
<property name="x"
value="0" />
<property name="y"
value="20" />
</bean>
</beans>
Triangle.java
package org.yash.watertechsol;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Triangle{
private Point pointA;
private Point pointB;
private Point pointC;
public Point
getPointA() {
return pointA;
}
public void setPointA(Point
pointA) {
this.pointA = pointA;
}
public Point
getPointB() {
return pointB;
}
public void setPointB(Point
pointB) {
this.pointB = pointB;
}
public Point
getPointC() {
return pointC;
}
public void setPointC(Point
pointC) {
this.pointC = pointC;
}
public void draw(){
System.out.println("Point A
= (" + getPointA().getX()+", "+getPointA().getY()+")");
System.out.println("Point B
= (" + getPointB().getX()+", "+getPointB().getY()+")");
System.out.println("Point C
= (" + getPointC().getX()+", "+getPointC().getY()+")");
}
public void myInit(){
System.out.println("My Init
method called");
}
public void cleanUp(){
System.out.println("My
CleanUp method called");
}
/*
@Override
public void destroy()
throws Exception {
System.out.println("CleanUp
Code will be called here");
}
@Override
public void
afterPropertiesSet() throws Exception {
System.out.println("Initialization
code will be called here");
}
*/
}
Output:
My Init method called
Point A = (0, 0)
Point B = (-20, 0)
Point C = (0, 20)
My CleanUp method called
If you want to implement both spring call back and bean call
back methods. Spring call back methods are first priority.
Output:
Initialization code will be called here
My Init method called
Point A = (0, 0)
Point B = (-20, 0)
Point C = (0, 20)
CleanUp Code will be called here
My CleanUp method called
If you want to add default Init and Destroy methods
to all beans then you need to configure in <beans> tag.
<beans default-init-method="myInit" default-destroy-method="cleanUp">
No comments:
Post a Comment