There are couple of ways to achieve this requirement.
First methodology
Youcan simply implements below interfaces in the bean
Note: You have to use registerShutdownHook (link) to see the effect of the destroy method.
Second methodology
In this scenario, I would like to introduce the way we can execute the custom method when initializing and destroying beans.
Initially, you have to implement custom methods forinit and destroy as below
In the bean configuration file you have to tell to the Spring what are theinit and destroy methods as below.
Enjoy...!!!
First methodology
You
InitializingBean DisposableBean
Then you have to override below methods respectively above interfaces.
afterPropertiesSet ( )destroy ( )
Second methodology
In this scenario, I would like to introduce the way we can execute the custom method when initializing and destroying beans.
Initially, you have to implement custom methods for
public void myInit( ) {System. out. println ( "This is the custominit method");}....public voidmyDestroy ( ) {System. out. println ( "This is the custom destroy method");}
In the bean configuration file you have to tell to the Spring what are the
<beans ><bean id="student" class="com . home. model. Student" init-method="myInit " destroy-method="myDestroy ">.....</bean >..........</beans >
Enjoy...!!!
No comments:
Post a Comment