Spring allows a developer to provide initialization and destruction callback methods so that they can call them during the life cycle of the bean. If one has provided those methods, spring will invoke them at the appropriate time.
The Spring container guarantees that a configured initialization callback is called immediately after all their properties have been set by a BeanFactory
.
If a bean wants to release resources on destruction, it needs to provide a destruction callback method. When an application context is closed, it will dispose all of its singleton so if a destruction callback method is provided then it will be invoked by a BeanFactory
on destruction of the singleton.
In this article, we will see some examples of initialization and destruction callback methods. We will also see how one can specify custom init and destroy methods and override the default ones.
This example uses the following frameworks:
Dependencies
Add the following dependencies:
spring-core
spring-context
Default init and destroy methods
Here is the test bean with the init()
and destroy()
methods. You can specify the initialization method in default-init-method
attribute on the top-level element and likewise destruction callback in default-destroy-method
.
TestBean:
package com.javarticles.spring; public class TestBean { private boolean isInitMethodCalled; private boolean isDestroyMethodCalled; private boolean isInitMethodOverriden; private boolean isDestroyMethodOverriden; public void init() { System.out.println("init() called"); isInitMethodCalled = true; } public void destroy() { System.out.println("destroy() called"); isDestroyMethodCalled = true; } public boolean isInitMethodCalled() { return isInitMethodCalled; } public boolean isDestroyMethodCalled() { return isDestroyMethodCalled; } }
defaultInitDestroyMethods.xml:
<?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" default-init-method="init" default-destroy-method="destroy"> <bean id="defaultInitDestroyMethods" class="com.javarticles.spring.TestBean"/> </beans>
We load the context and get the defaultInitDestroyMethods
and verify whether the init()
method is called. In the finally
block, we close the context which will in turn destroy the singleton bean and this is when spring allows the bean to release its resources by invoking the destruction callback destroy()
.
SpringDefaultInitDestroyMethodsExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDefaultInitDestroyMethodsExample { public static void main(String[] args) { System.out.println("Bean with init and destory methods"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "defaultInitDestroyMethods.xml"); TestBean testBean = (TestBean) context.getBean("defaultInitDestroyMethods"); try { System.out.println("Init Method called? " + testBean.isInitMethodCalled()); } finally { context.close(); System.out.println("Destroy Method called? " + testBean.isDestroyMethodCalled()); } } }
Output:
Bean with init and destory methods init() called Init Method called? true destroy() called Destroy Method called? true
Default init and destroy methods disabled
In this example, we will disable the default initialization and destruction callbacks by providing empty init-method
and destroy-method
at the bean
level element.
initDestroyMethodsDisabled.xml:
<?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" default-init-method="init" default-destroy-method="destroy"> <bean id="initDestroyMethodsDisabled" class="com.javarticles.spring.TestBean" init-method="" destroy-method=""/> </beans>
The initialization and destruction callbacks shouldn’t be called as they are disabled.
SpringInitDestroyMethodsDisabledExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringInitDestroyMethodsDisabledExample { public static void main(String[] args) { System.out.println("Init and destory methods disabled"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "initDestroyMethodsDisabled.xml"); TestBean testBean = (TestBean) context.getBean("initDestroyMethodsDisabled"); try { System.out.println("Init Method called? " + testBean.isInitMethodCalled()); } finally { context.close(); System.out.println("Destroy Method called? " + testBean.isDestroyMethodCalled()); } } }
Output:
Init and destory methods disabled Init Method called? false Destroy Method called? false
Override default init and destroy methods
Let’s add new methods initOverriden()
. and destroyOverriden()
to TestBean
. We will use these methods to override the default init()
and destroy()
methods.
TestBean:
package com.javarticles.spring; public class TestBean { private boolean isInitMethodCalled; private boolean isDestroyMethodCalled; private boolean isInitMethodOverriden; private boolean isDestroyMethodOverriden; public void init() { System.out.println("init() called"); isInitMethodCalled = true; } public void destroy() { System.out.println("destroy() called"); isDestroyMethodCalled = true; } public boolean isInitMethodCalled() { return isInitMethodCalled; } public boolean isDestroyMethodCalled() { return isDestroyMethodCalled; } public void initOverriden() { System.out.println("initOverriden() called"); isInitMethodOverriden = true; } public void destroyOverriden() { System.out.println("destroyOverriden() called"); isDestroyMethodOverriden = true; } public boolean isInitMethodOverriden() { return isInitMethodOverriden; } public boolean isDestroyMethodOverriden() { return isDestroyMethodOverriden; } }
In this example, we will provide custom init and destroy method at the bean
level. This will override the default attributes default-init-method
and default-destroy-method
.
initDestroyMethodsOverriden.xml:
<?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" default-init-method="init" default-destroy-method="destroy"> <bean id="initDestroyMethodsOverriden" class="com.javarticles.spring.TestBean" init-method="initOverriden" destroy-method="destroyOverriden"/> </beans>
The default init and destroy method will no more be called and the custom methods initOverriden()
and destroyOverriden()
will be called.
SpringInitDestroyMethodsOverridenExample:
package com.javarticles.spring; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringInitDestroyMethodsOverridenExample { public static void main(String[] args) { System.out.println("Example of default init and destory methods getting overriden"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "initDestroyMethodsOverriden.xml"); TestBean testBean = (TestBean) context.getBean("initDestroyMethodsOverriden"); try { System.out.println("Default Init Method called? " + testBean.isInitMethodCalled()); } finally { context.close(); System.out.println("Default Destroy Method called? " + testBean.isDestroyMethodCalled()); } } }
Output:
Example of default init and destory methods getting overriden initOverriden() called Default Init Method called? false destroyOverriden() called Default Destroy Method called? false
Download the source code
This was an example about spring initialization and destruction callback methods. We have shown examples to set the default-init
and default-destroy
methods and how one can override them in bean
configuration. You can download the source code here: springInitDestroyMethodsExample.zip