Spring Default Initialization and Destruction Callback

Spring provides a way to define default initialization and destruction for all beans defined in bean configuration file using default-init-method and default-destroy-method attributes on <bean> element. You don’t need to declare init-method and destroy-method on each individual bean.

Following example demonstrates default initialization and destruction callback in Spring

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency> 
  </dependencies>

1. POJO

Create Employee Class as following

package com.kruders.bean;

public class Employee{
	private String name;
     
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public void init() throws Exception {
    	System.out.println("Init method called: ");
  	}
    
    public void destroy() throws Exception {
  	  	System.out.println("Destroy method called: ");
  	}
}

2. Configuration

Create Spring-Bean.xml and write the following code

<?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="employee" class="com.kruders.bean.Employee">
        <property name="name" value="Puneet" />
    </bean>

	<bean id="employee2" class="com.kruders.bean.Employee">
        <property name="name" value="Rahul" />
    </bean>
</beans>

3. Run Program

Create Main.java class and run it as Java Application

package com.kruders.core;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.kruders.bean.Employee;
 
public class Main {
    public static void main(String[] args) {
    	ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
                "Spring-Bean.xml");
        Employee employee = (Employee) context.getBean("employee");
        System.out.println("Name " + employee.getName());
        
        Employee employee2 = (Employee) context.getBean("employee2");
        System.out.println("Name " + employee.getName());
     
        context.close();
    }
 
}

The folder structure of the example is shown below in Figure 56.1

Figure 56.1 Figure 56.1

You can download the source code of this example here.

No comments yet.

Leave a Reply