Wednesday 26 April 2017

Spring: InitializingBean and DisposableBean example

Spring provides InitializingBean and DisposableBean interfaces to handle bean life cycle call backs.

InitializingBean interface
InitializingBean interface provides 'afterPropertiesSet' method, it is called by the bean factory after it has set all bean properties supplied.

DisposableBean interface
DisposableBean interface provides destroy method, it is called by a BeanFactory on destruction of a singleton bean.

Following is the complete working application.

Author.java
package com.sample.pojo;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Author implements InitializingBean, DisposableBean {
 private String firstName;
 private String lastName;
 private String dateOfBirth;
 private String country;

 public Author() {
  System.out.println("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 afterPropertiesSet() throws Exception {
  System.out.println("\n**************************");
  System.out.println("Following properties are set to author bean");
  System.out.println("firstName : " + firstName);
  System.out.println("lastName : " + lastName);
  System.out.println("dateOfBirth : " + dateOfBirth);
  System.out.println("country : " + country);
  System.out.println("**************************\n");
 }

 @Override
 public void destroy() throws Exception {
  System.out.println("\n**************************");
  System.out.println("Destroying bean " + this);
  System.out.println("**************************");
 }

}

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">

 <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>

</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Author;

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

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

  System.out.println(osho);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output
Constructor called

**************************
Following properties are set to author bean
firstName : Chandra Mohan
lastName : Jain
dateOfBirth : 11 December 1931
country : India
**************************

Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]

**************************
Destroying bean Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
**************************

Note
a. It is recommended that you do not use the InitializingBean interface because it unnecessarily couples the code to Spring. Alternatively, use the @PostConstruct annotation or specify a POJO initialization method. In the case of XML-based configuration metadata, you use the init-method attribute to specify the name of the method that has a void no-argument signature. With Java config, you use the initMethod attribute of @Bean.

public class Author {

         public void initCheck() {
                 System.out.println("\n**************************");
                 System.out.println("Following properties are set to author bean");
                 System.out.println("firstName : " + firstName);
                 System.out.println("lastName : " + lastName);
                 System.out.println("dateOfBirth : " + dateOfBirth);
                 System.out.println("country : " + country);
                 System.out.println("**************************\n");

         }

}

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

Update Author.java, myConfiguration.xml like below.

Author.java
package com.sample.pojo;

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

 public Author() {
  System.out.println("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();
 }

 public void initCheck() {
  System.out.println("\n**************************");
  System.out.println("Following properties are set to author bean");
  System.out.println("firstName : " + firstName);
  System.out.println("lastName : " + lastName);
  System.out.println("dateOfBirth : " + dateOfBirth);
  System.out.println("country : " + country);
  System.out.println("**************************\n");

 }

}

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">

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

</beans>


HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Author;

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

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

  System.out.println(osho);

  ((ClassPathXmlApplicationContext) context).close();
 }
}

Output
Constructor called

**************************
Following properties are set to author bean
firstName : Chandra Mohan
lastName : Jain
dateOfBirth : 11 December 1931
country : India
**************************

Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]

b. It is recommended that you do not use the DisposableBean callback interface because it unnecessarily couples the code to Spring. Alternatively, use the @PreDestroy annotation or specify a generic method that is supported by bean definitions. With XML-based configuration metadata, you use the destroy-method attribute on the <bean/>. With Java config, you use the destroyMethod attribute of @Bean.
public class Author {

         public void cleanup() {
                 System.out.println("\n**************************");
                 System.out.println("Cleaning up the bean " + this);
                 System.out.println("**************************\n");
         }

}

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

Author.java
package com.sample.pojo;

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

 public Author() {
  System.out.println("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();
 }

 public void cleanup() {
  System.out.println("\n**************************");
  System.out.println("Cleaning up the bean " + this);
  System.out.println("**************************\n");
 }

}

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">

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

</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Author;

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

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

  System.out.println(osho);

  ((ClassPathXmlApplicationContext) context).close();
 }
}


Output
Constructor called
Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]

**************************
Cleaning up the bean Author [firstName=Chandra Mohan, lastName=Jain, dateOfBirth=11 December 1931, country=India]
**************************


Previous                                                 Next                                                 Home

No comments:

Post a Comment