Table of Contents
In this post, we will see about Spring init-method and destroy-method.
When bean is instantiated or destroyed , there are some operations you may need to perform, so you can use init-method and destroy-method to call this methods while bean is being created or destroyed.
Lets understand it with the help of simple example:
For configuring spring in your eclipse ide please refer hello world example
Here you need to register a shutdown hook registerShutdownHook() method that is declared on the AbstractApplicationContext class. This will ensures a graceful shutdown and calls the relevant destroy methods.
When bean is instantiated or destroyed , there are some operations you may need to perform, so you can use init-method and destroy-method to call this methods while bean is being created or destroyed.
Lets understand it with the help of simple example:
For configuring spring in your eclipse ide please refer hello world example
1.Country.java:
This is simple pojo class which have only one attribute as countryName.
Create Country.java under package org.arpit.java2blog.Copy following content into Country.java.
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 |
package org.arpit.java2blog; public class Country { String countryName ; public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public void init() { System.out.println("In init block of country"); } public void destroy() { System.out.println("In destroy block of country"); } } |
2.SpringInitDestroyMain.java
This class contains main function.Create SpringInitDestroyMain.java under package org.arpit.java2blog.Copy following content into SpringInitDestroyMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringInitDestroyMain{ public static void main(String[] args) { AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Country countryObj = (Country) appContext.getBean("country"); System.out.println("Country Name: "+countryObj.getCountryName()); appContext.registerShutdownHook(); } } |
3.ApplicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="country" class="org.arpit.javapostsforlearning.Country" init-method="init" destroy-method="destroy"> <property name="countryName" value="India"/> </bean> </beans> |
4.Run it
When you will run above application,you will get following as output.
1 2 3 4 5 |
In init block of country Country Name: India In destroy block of country |
Default Initialization and destroy method:
If you want to put default methods for all beans and do not want to configure of individual beans, you can use default-init-method and default-destroy-method with tag, so it will call init and destroy methods for all beans configured in ApplicationContext.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-init-method="init" default-destroy-method="destroy" > <bean id="country" class="org.arpit.javapostsforlearning.Country"> <property name="countryName" value="India"/> </bean> </beans> |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.