Sunday, March 20, 2011

@PostConstruct and @PreDestroy example

In this tutorial you will learn how to implement the @PostConstruct and @PreDestroy which work similar to init-method and destroy-method in bean configuration file or implement the InitializingBean and DisposableBean in your bean class. To use @PostConstruct and @PreDestroy you have to register the CommonAnnotationBeanPostProcessor at bean configuration or specifying the <context:annotation-config /> in the bean configuration file.

Consider the service bean:

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

public class StudentService {

String message;

public String getMessage() {
return message;

public void setMessage(String message) {
this.message = message;

public void initIt() throws Exception {
System.out.println("After properties has been set : " + message);

public void cleanUp() throws Exception {
System.out.println("Cleaned Everyting");


Bean config file:

<beans xmlns=""

<context:annotation-config />

<bean id="studentService" class="com.xxxx.StudentService">
<property name="message" value="property message" />


No comments:

Post a Comment