Home  >  Spring Core

@PostConstruct and @PreDestroy Annotation in Spring Example

By Arvind Rai, February 13, 2013
On this page we will provide @PostConstruct and @PreDestroy annotation example in spring. These annotations are annotation form of init-method and destroy-method attribute of bean tag in XML. The method annotated with @PostConstruct is called once the bean properties are initialized and the method annotated with @PreDestroy is called before the spring container destroys the instance of bean. For init-method and destroy-method bean attribute, find the link.

Spring init-method and destroy-method Example using XML

Now find the complete example for @PostConstruct and @PreDestroy annotation.

@PostConstruct

@PostConstruct annotation defines a method as initialization method of a spring bean which runs after dependency injection is completed. @PostConstruct is annotation form of init-method which is an attribute of bean tag. @PostConstruct method is used to validate properties of bean or initializing any task. In our bean, there must be only one method annotated with @PostConstruct annotation. The method cannot be static.

@PreDestroy

@PreDestroy annotation declares a method that is executed just before the bean is destroyed by container. The method annotated with @PreDestroy is used to release the resources or preforming any destruction task before the spring container destroyed the bean. There must be only one method in our bean annotated with @PreDestroy. The method cannot be static. @PreDestroy is annotation form of destroy-method which is an attribute of bean tag.


Create Bean with @PostConstruct and @PreDestroy

We are creating a class with the methods annotated with @PostConstruct and @PreDestroy.
Book.java
package com.concretepage;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Book {
	private String bookName;
	public Book(String bookName) {
		this.bookName = bookName;
	}
	@PostConstruct
	public void springPostConstruct() {
	     System.out.println("---Do initialization task---");
	     if(bookName != null) {
	    	 System.out.println("bookName property initialized with the value:"+ bookName);
	     }
	}	
	public String getBookName() {
		return bookName;
	}
	@PreDestroy
	public void springPreDestroy() {
		System.out.println("---Release resources or perform destruction task---");
	}
} 

Java Configuration Class

Find the configuration class that is defining the bean.
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
        @Bean	
	public Book book(){
		return new Book("Ramayana");
	}
} 

Main Class

To test the application we are initializing the container and then closing it.
SpringDemo.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
	    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	    ctx.register(AppConfig.class);
	    ctx.refresh();
	    Book book = ctx.getBean(Book.class);
	    System.out.println("Book name:" + book.getBookName());
	    ctx.close();
	}
} 
Find the output.
---Do initialization task---
bookName property initialized with the value:Ramayana
Book name:Ramayana
---Release resources or perform destruction task--- 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us