Spring Tutorial - Spring Bean Initialize and Destroy








We can use the Spring InitializingBean and DisposableBean to perform actions on bean initialization and destruction.

For a bean implemented InitializingBean from Spring, the Spring IoC container will run afterPropertiesSet() method after all bean properties have been set.

For a bean implemented DisposableBean from Spring, it will call destroy() after Spring container is released the bean.

Java Bean

Here's an example to show you how to use InitializingBean and DisposableBean.

A PrinterHelper bean is defined in the following code and it implements both InitializingBean and DisposableBean interface.

It also has two more methods one is afterPropertiesSet() method and the other is destroy() method.

package com.java2s.common;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class PrinterHelper implements InitializingBean, DisposableBean
{
  String message;
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
  public void afterPropertiesSet() throws Exception {
    System.out.println("Init method after properties are set : " + message);
  }
  public void destroy() throws Exception {
    System.out.println("Spring Container is destroy! JavaBean clean up");
  }
  @Override
  public String toString() {
    return  message ;
  }  
}




XML configuration file

Here is the Spring-Customer.xml for Java bean configuration.

<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="printerService" class="com.java2s.common.PrinterHelper">
    <property name="message" value="i'm property message" />
       </bean>
</beans>




Main method

Here is the code shows how to run the code above.

ConfigurableApplicationContext.close() call will close the application context and it will call destroy() method.

package com.java2s.common;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App 
{
    public static void main( String[] args )
    {
      ConfigurableApplicationContext context = 
      new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
      PrinterHelper cust = (PrinterHelper)context.getBean("printerService");
      System.out.println(cust);
      context.close();
    }
}

The code above generates the following result.

From the output we can see that the afterPropertiesSet() method is called, after the message property is set and the destroy() method is call after the context.close();



Download Java2s_Spring_Init_Destroy.zip

Spring init-method and destroy-method

The following code shows another way to call custom method in a Java bean when initializing and destroying a Java Bean.

We can use init-method and destroy-method attribute in bean configuration file to mark init method and destroy method in Java Bean.

The init-method is called during initialization and the destroy-method is called during destruction.

The following code shows how to add methods in a Java Bean to do init and destroy respectively.

package com.java2s.common;
public class PrinterHelper {
  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");
  }

  @Override
  public String toString() {
    return message;
  }

}

In the following xml configuration file it uses init-method and destroy-method attributes to mark the init method and destroy method repectively.

<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="printerService" class="com.java2s.common.PrinterHelper" 
    init-method="initIt" destroy-method="cleanUp">
    <property name="message" value="i'm property message" />
  </bean>
</beans>


Download Java2s_Spring_init-method.zip

@PostConstruct and @PreDestroy annotation

Spring also support the @PostConstruct and @PreDestroy annotation marked Java Bean.

We use those two annotations to mark init-method and destroy-method.

@PostConstruct and @PreDestroy annotation are from the J2ee common-annotations.jar.

In the following code we use @PostConstruct and @PreDestroy annotations to mark the PrinterHelper class.

package com.java2s.customer.services;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class PrinterHelper
{
  String message;
  public String getMessage() {
    return message;
  }
  public void setMessage(String message) {
    this.message = message;
  }
  @PostConstruct
  public void initIt() throws Exception {
    System.out.println("Init method after properties are set : " + message);
  }
  @PreDestroy
  public void cleanUp() throws Exception {
    System.out.println("Spring Container is destroy! Customer clean up");
  }
}

In order to use @PostConstruct and @PreDestroy annotations we need to either register 'CommonAnnotationBeanPostProcessor' or specify the '<context:annotation-config />' in bean configuration file.

The following xml configuraiton file calls CommonAnnotationBeanPostProcessor Java 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 class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  <bean id="printerService" class="com.java2s.common.PrinterHelper">
    <property name="message" value="i'm property message" />
  </bean>
</beans>

The following xml file calls <context:annotation-config />.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  <context:annotation-config />
  <bean id="printerService" class="com.java2s.common.PrinterHelper">
    <property name="message" value="i'm property message" />
  </bean>
</beans>