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.


@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
  4. Spring 3.2.0


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" 



    <!-- Define Spring version as a constant -->



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);
  public void initialize(){
    System.out.println("After bean initialization");
  public void cleanup(){
    System.out.println("Cleaning up");


Spring configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

  <context:annotation-config />

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



A simple test:

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

    // Explicitly closing application 
    // context to force bean cleanup

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


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: