Spring @PostConstruct and @PreDestroy example
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:
- Ubuntu 12.04
- Maven 3.0.4
- JDK 1.7.0.09
- Spring 3.2.0
Configuration
Configure Maven to get the required Spring 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:
After conclusion you can import the project into Eclipse.
@PostConstruct and @PreDestroy
Let's define an example bean using @PostConstruct and @PreDestroy annotations:
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:
<?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:
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:
Bean method called. Text is: some text
Cleaning up
This tutorial source code can be found at the end of this page.