In Spring, you can either implements InitializingBean and DisposableBean interface or specify the init-method and destroy-method in bean configuration file for the initialization and destruction callback function. In this article, we show you how to use annotation @PostConstruct and @PreDestroy to do the same thing.
The @PostConstruct and @PreDestroy annotation are not belong to Spring, it’s located in the J2ee library – common-annotations.jar.

@PostConstruct and @PreDestroy

A CustomerService bean with @PostConstruct and @PreDestroy annotation
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class CustomerService
 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");
By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register ‘CommonAnnotationBeanPostProcessor‘ or specify the ‘‘ in bean configuration file,

1. CommonAnnotationBeanPostProcessor

  class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  id="customerService" class="">
   name="message" value="i'm property message" />