Custom init() and destroy() methods in Spring

Introduction

In this tutorial we will discuss on Spring custom init() and destroy() methods. These methods are call back methods which used in Spring life cycle.

You can use these methods to to some initialization and clean up jobs just after the bean is created by the Spring container or just before the bean is about to be destroyed by the Spring container respectively.

We can also use @PostConstruct and @PreDestroy annotations in the Spring life cycle event to perform the similar kind of jobs.

In this example we will see both local and global configurations for init() and destroy() methods.

We will also create both XML and Annotation based bean configurations for these call back methods.

At present there is no suitable annotation provided by Spring framework by which you can apply init or destroy methods globally but there is a workaround on this using custom annotation and I will show you. Though you have to use the custom annotation on every bean on which you want to apply but it will reduce your efforts on @Bean(initMethod="", ) and instead of this annotation you just need to use the custom annotation.

So if you do not want to use my way of global configuration using annotation then you have one of two options left, either apply @Bean(initMethod="", ) on all beans where you want to apply or use XML based configuration.

Local configuration means the init and destroy methods will be applicable to the single bean on which you apply init and destroy methods.

Global configuration means the init and destroy methods will be applicable to multiple beans on which you apply init and destroy methods.

Annotation Based Configuration

Local Configuration

@Configuration
public class ConfigLocal {

	@Bean(initMethod = "init", destroyMethod = "destroy")
	public Message message() {
		return new Message("Welcome to local init() and destroy()");
	}

}

Global Configuration

We first create a custom annotation:

@Retention(RetentionPolicy.RUNTIME)
@Bean(initMethod = "init", destroyMethod = "destroy")
public @interface DefaultInitDestroy {

}

Now you just need to use @DefaultInitDestroy annotation on the beans where you want to apply init and destroy methods.

Remember you need to define init and destroy methods with specific behavior for each bean on which you are applying @DefaultInitDestroy annotation.

@Configuration
public class ConfigGlobal {

	@DefaultInitDestroy
	public InitDestroyBean initDestroyBean() {
		return new InitDestroyBean("Welcome to global init() and destroy()");
	}

	@DefaultInitDestroy
	public AnotherInitDestroyBean anotherInitDestroyBean() {
		return new AnotherInitDestroyBean("Welcome to another global init() and destroy()");
	}

}

XML Based Configuration

Local Configuration

We specify init-method and destroy-method on bean level.

<?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">
	<bean id="message"
		class="com.roytuts.spring.init.destroy.methods.Message"
		init-method="init" destroy-method="destroy">
		<constructor-arg
			value="Welcome to local init() and destroy()" />
	</bean>
</beans>

Global Configuration

We specify default-init-method and default-destroy-method on beans level.

<?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="initDestroyBean"
		class="com.roytuts.spring.init.destroy.methods.InitDestroyBean">
		<constructor-arg
			value="Welcome to global init() and destroy()" />
	</bean>

	<bean id="anotherInitDestroyBean"
		class="com.roytuts.spring.init.destroy.methods.AnotherInitDestroyBean">
		<constructor-arg
			value="Welcome to another global init() and destroy()" />
	</bean>
</beans>

Testing the Application

Local configuration will give you the following output for both XML and Java based configuration:

Local init() - doing some initialization stuff
Welcome to local init() and destroy()
Local destroy() - doing some clean up stuff

Global configuration will give you the following output for both XML and Java based configuration:

Global init() - InitDestroyBean - doing some initialization stuff
Global init() - AnotherInitDestroyBean - doing some initialization stuff
Welcome to global init() and destroy()
Welcome to another global init() and destroy()
Global destroy() - AnotherInitDestroyBean - doing some clean up stuff
Global destroy() - InitDestroyBean - doing some clean up stuff

Source Code

Download

Thanks for reading.

Related posts

Leave a Comment