Spring @PostConstruct and @PreDestroy example

10 January 2013
By Gonçalo Marques
In this tutorial you will learn how to use @PostConstruct and @PreDestroy annotations with Spring.

Introduction

@PostConstruct and @PreDestroy annotations are not exclusive to Spring: they are a standard and consequently widely used in many container managed environments. Spring container is no exception so you can use these annotations in your Spring beans.

@PostConstruct annotation defines a method that will be called after a bean as been fully initialized. In other words it will be called after bean construction and all dependency injection.

@PreDestroy annotation defines a method that will be called just before a bean is destroyed. This is usually useful for resource clean up.

This tutorial considers the following software and environment:

  1. Ubuntu 12.04
  2. Maven 3.0.4
  3. JDK 1.7.0.09
  4. Spring 3.2.0

Configuration

Configure Maven to get the required Spring dependencies:

Maven pom.xml file referencing required dependencies
<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.byteslounge.spring</groupId>
  <artifactId>com-byteslounge-spring</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>com-byteslounge-spring</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- Define Spring version as a constant -->
    <spring.version>3.2.0.RELEASE</spring.version>
  </properties>

  <dependencies>
  
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

  </dependencies>
</project>

Now place yourself in the project directory and issue the following command to prepare your project for Eclipse:

mvn eclipse:eclipse

After conclusion you can import the project into Eclipse.

@PostConstruct and @PreDestroy

Let's define an example bean using @PostConstruct and @PreDestroy annotations:

@PostConstruct and @PreDestroy
package com.byteslounge.spring;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class ExampleBean {

  private String text;
  
  public String getText() {
    return text;
  }

  public void setText(String text) {
    this.text = text;
  }

  public void doSomething(){
    System.out.println("Bean method called. Text is: " + text);
  }
  
  @PostConstruct
  public void initialize(){
    System.out.println("After bean initialization");
  }
  
  @PreDestroy
  public void cleanup(){
    System.out.println("Cleaning up");
  }

}


Spring configuration file:

spring.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"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:annotation-config />

  <bean id="exampleBean" class="com.byteslounge.spring.ExampleBean">
    <property name="text" value="some text" />
  </bean>

</beans>

Testing

A simple test:

Main.java
package com.byteslounge.spring;

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

public class Main {
  public static void main( String[] args ) {
    ApplicationContext ctx = 
       new ClassPathXmlApplicationContext("spring.xml");
    ExampleBean exampleBean = (ExampleBean) ctx.getBean("exampleBean");
    exampleBean.doSomething();

    // Explicitly closing application 
    // context to force bean cleanup
    ((ClassPathXmlApplicationContext)ctx).close();    
  }
}

When we run our test the following output will be generated:

After bean initialization

Bean method called. Text is: some text

Cleaning up


This tutorial source code can be found at the end of this page.

Download source code from this article

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: