Main Tutorials

Spring init-method and destroy-method example

In Spring, you can use init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction. Alternative to InitializingBean and DisposableBean interface.

Example

Here’s an example to show you how to use init-method and destroy-method.


package com.mkyong.customer.services;

public class CustomerService
{
	String message;
	
	public String getMessage() {
	  return message;
	}

	public void setMessage(String message) {
	  this.message = message;
	}
	
	public void initIt() throws Exception {
	  System.out.println("Init method after properties are set : " + message);
	}
	
	public void cleanUp() throws Exception {
	  System.out.println("Spring Container is destroy! Customer clean up");
	}
	
}

File : Spring-Customer.xml, define init-method and destroy-method attribute in your bean.


<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-2.5.xsd">

	<bean id="customerService" class="com.mkyong.customer.services.CustomerService" 
		init-method="initIt" destroy-method="cleanUp">
   		
		<property name="message" value="i'm property message" />
	</bean>
		
</beans>

Run it


package com.mkyong.common;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App 
{
    public static void main( String[] args )
    {
    	ConfigurableApplicationContext context = 
		new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
	
    	CustomerService cust = (CustomerService)context.getBean("customerService");
    	
    	System.out.println(cust);
    	
    	context.close();
    }
}

The ConfigurableApplicationContext.close will close the application context, releasing all resources and destroying all cached singleton beans.

Output


Init method after properties are set : i'm property message
com.mkyong.customer.services.CustomerService@47393f
...
INFO: Destroying singletons in org.springframework.beans.factory.
support.DefaultListableBeanFactory@77158a: 
defining beans [customerService]; root of factory hierarchy
Spring Container is destroy! Customer clean up

The initIt() method is called, after the message property is set, and the cleanUp() method is called after the context.close();

Thoughts…
It’s always recommended to use init-method and destroy-method in bean configuration file, instead of implement the InitializingBean and DisposableBean interface to cause unnecessarily coupled your code to Spring.

Download Source Code

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
tukaram
2 years ago

Is it mandatory to have a init method?

Akash
1 year ago
Reply to  tukaram

No in Spring both init and destroy methods are optional to take

Kevin
4 years ago

Hello Sir, this works well for singletons. If you also want to have a destroy()-method on prototypes, then https://mkyong.com/spring/spring-initializingbean-and-disposablebean-example/ is a solution. Somestimes you want to track prototypes through their whole lifecycle.

Pooja
5 years ago

what will be behaviors if initIt() is private.

Vikram Pyati
10 years ago

Hi,

Is the init-method threadsafe ?

Matthias
11 years ago

as always … a valuable source!

Thx a lot

Yogesh
11 years ago

hi,

Can you explain if destroy method required some parameter how i can write in Spring-Customer.xml

public void cleanUp(String down) throws Exception {
System.out.println(“Spring Container is destroy! Customer clean up”+ down);
}

thanks yogesh

Somapala
10 years ago
Reply to  Yogesh

destroy methods does not accept non boolean parameters. But you can inject via setter or constructor and use in the destroy method.