Saturday, 29 April 2017

Spring: default-init-method & default-destroy-method Example

In my previous posts, I explained how to set the life cycle methods at bean level using init-method, destroy-method attributes. Suppose if you want to set the life cycle methods for all the beans at a time, you can do this by using the attributes default-init-method & default-destroy-method of beans element.

Example
<?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="cleanup">
        
</beans>

You need to define the method init, cleanup in your application logic. Spring container call these methods on bean creation and destroy time. Following is the complete working application.

LifeCycleService.java
package com.sample.pojo;

public interface LifeCycleService {
 public void init();
 
 public void cleanup();
}

Student.java
package com.sample.pojo;

public class Student implements LifeCycleService{
 private String name;
 private int id;
 
 public Student(){
  System.out.println("Student constructor called");
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Student [name=").append(name).append(", id=").append(id).append("]");
  return builder.toString();
 }

 @Override
 public void init() {
  System.out.println(this + " is initialized");
 }

 @Override
 public void cleanup() {
  System.out.println(this + " is about to destroy");
 }
 
}

Author.java
package com.sample.pojo;

public class Author implements LifeCycleService {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public Author() {
  System.out.println("Author Constructor called");
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getDateOfBirth() {
  return dateOfBirth;
 }

 public void setDateOfBirth(String dateOfBirth) {
  this.dateOfBirth = dateOfBirth;
 }

 public String getCountry() {
  return country;
 }

 public void setCountry(String country) {
  this.country = country;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
    .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
  return builder.toString();
 }

 @Override
 public void init() {
  System.out.println(this + " is initialized");
 }

 @Override
 public void cleanup() {
  System.out.println(this + " is about to destroy");
 }

}

myConfiguration.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="cleanup">

 <bean id="osho" class="com.sample.pojo.Author">
  <property name="firstName" value="Chandra Mohan" />
  <property name="lastName" value="Jain" />
  <property name="dateOfBirth" value="11 December 1931" />
  <property name="country" value="India" />
 </bean>

 <bean id="krishna" class="com.sample.pojo.Student">
  <property name="name" value="Hari krishna" />
  <property name="id" value="553" />
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Author;
import com.sample.pojo.Student;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Author osho = context.getBean("osho", Author.class);
  Student krishna = context.getBean("krishna", Student.class);

  System.out.println(osho);
  System.out.println(krishna);
  
  ((ClassPathXmlApplicationContext) context).close();
 }
}


Output
Author Constructor called
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India] is initialized
Student constructor called
Student [name=Hari krishna, id=553] is initialized
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
Student [name=Hari krishna, id=553]
Student [name=Hari krishna, id=553] is about to destroy
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India] is about to destroy







Previous                                                 Next                                                 Home

No comments:

Post a Comment