<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div style="color: #274e13;">In this chapter you will learn how to implement the <i><b>@PostConstruct</b></i> and <i><b>@PreDestroy</b></i> which work similar to <b>init-method</b> and <b>destroy-method</b> in bean <b>configuration file</b> <b> (spring.xml)</b> <b><a href="https://dineshonjava.com/2012/07/bean-lifecycle-and-callbacks.html">describe in earlier chapter</a></b> or implement the <b>InitializingBean </b>and <b>DisposableBean </b>in your bean class.</p>
<div align='center' id="ads-id"></div>
<p>To ; ; ; use ; ; ; <i><b>@PostConstruct</b></i> ; ; and ; ; ; <i><b> @PreDestroy  ; ;</b></i> you ; ; have  ; ; to ; ; ; register  ; the <b>CommonAnnotationBeanPostProcessor</b>  ; at ; ; bean ; ; configuration  ; or  ; ; specifying  ; the <b><;context:annotation-config />;</b> in the bean <b>configuration file</b> <b> (spring.xml)</b>. </div>
<div style="color: #274e13;">
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;"><b>Popular Tutorials</b></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-web-mvc-framework-chapter-38.html"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2016/06/introduction-to-spring-boot-a-spring-boot-complete-guide.html"><em><strong>Spring Boot Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/02/spring-security-take-baby-step-to-secure.html"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint.html"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/using-spring-jdbc-framework-chapter-32.html"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/spring-hateoas-hypermedia-driven-restful-web-service.html"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/microservices-with-spring-boot.html"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/06/jax-rs-web-service-tutorial.html"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/2013/01/core-java-baby-step-to-be-best-java-ian.html"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/03/hibernate-3-on-baby-steps.html"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-batch-process-with-example.html"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
</div>
<div style="color: #274e13;">To define setup and teardown for a bean, we simply declare the with <b>init-method and/or destroy-method</b> parameters. The <b>init-method </b>attribute specifies a method that is to be called on the bean immediately upon instantiation. Similarly, <b>destroy-method</b> specifies a method that is called just before a bean is removed from the <b><a href="https://dineshonjava.com/2012/06/spring-ioc-container.html">container</a></b>.</div>
<div style="color: #274e13;">
<b><b style="color: blue;">Note:</b><br />
The <i><b>@PostConstruct</b></i> and <i><b>@PreDestroy</b></i> annotation are not belong to Spring, it’s located in the J2ee library – <i>common-annotations.jar</i>. ;</b></div>
<div style="color: #274e13;">
 You can use <b>@PostConstruct</b> annotation as an alternate of <a href="https://dineshonjava.com/2012/07/bean-lifecycle-and-callbacks.html"><b>initialization callback</b></a> and <i><b>@PreDestroy</b></i> annotation as an alternate of <a href="https://dineshonjava.com/2012/07/bean-lifecycle-and-callbacks.html"><b>destruction callback</b></a> as explained in the below example.</div>
<p>
<b>Circle.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.jsr.tutorial;

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

public class Circle
{ 
 private Point center;
 
 @Resource(name="pointB")
 public void setCenter(Point center) 
 {
 this.center = center;
 }
 
 public void draw() 
 {
System.out.println("Circle is drawn of center ("+center.getX()+", "+center.getY()+")");
 }
 
 @PostConstruct
 public void initializeCircle()
 {
 //populates the circle data cache upon initialization... 
 System.out.println("Init of Circle");
 }
 
 @PreDestroy
 public void destroyCircle()
 {
 //clears the circle related cache upon destruction.. 
 System.out.println("Destroy of Circle");
 }
}
</pre>
<p><b>Point.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.jsr.tutorial;

public class Point
{
 private int x;
 private int y;
 /**
 * @return the x
 */
 public int getX() {
 return x;
 }
 /**
 * @param x the x to set
 */
 public void setX(int x) {
 this.x = x;
 }
 /**
 * @return the y
 */
 public int getY() {
 return y;
 }
 /**
 * @param y the y to set
 */
 public void setY(int y) {
 this.y = y;
 }
}
</pre>
<p><b>DrawingApp.java</b></p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.jsr.tutorial;

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

/**
 * @author Dinesh
 *
 */
public class DrawingApp 
{
 /**
 * @param args
 */
 public static void main(String[] args) 
 {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
context.registerShutdownHook();
Circle circle = (Circle) context.getBean("circle");
circle.draw();
 }
}
</pre>
<p></p>
<div style="color: #274e13;">By default, Spring will not aware of the <b><i>@PostConstruct</i></b> and <i><b>@PreDestroy</b></i> annotation. To enable it, you have to either register <b>&#8220;CommonAnnotationBeanPostProcessor&#8221;</b> or specify the <b><;context:annotation-config />;</b> in <b>bean configuration file(spring.xml)</b>,</div>
<p></p>
<div style="color: #274e13;"><b>1. Using CommonAnnotationBeanPostProcessor</b></div>
<p><b>spring.xml</b></p>
<pre class="highlight" name="code"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:></p>
<div style="color: #274e13;"><b>2. Using <;context:annotation-config />;</b></div>
<p><b>spring.xml</b></p>
<pre class="highlight" name="code"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:></p>
<div style="color: #274e13;"><b>Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message:</b><br />
<b><br />
</b></div>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">Jul 14, 2012 1:57:00 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh</div>
<div style="color: red;">INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@145d068: startup date [Sat Jul 14 01:57:00 IST 2012]; root of context hierarchy</div>
<div style="color: red;">Jul 14, 2012 1:57:00 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions</div>
<div style="color: red;">INFO: Loading XML bean definitions from class path resource [spring.xml]</div>
<div style="color: red;">Jul 14, 2012 1:57:00 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons</div>
<div style="color: red;">INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4e79f1: defining beans [circle,pointA,pointB,pointC,center,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy</div>
<p><b>Init of Circle</b><br />
<b>Circle is drawn of center (-20, 0)</b><br />
<b>Destroy of Circle</b></div>
</div>
<div style="text-align: center;"></div>
<p></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Spring Related Topics you may like</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/2012/10/interview-questions-on-spring.html">Spring Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-interview-questions-and-answers.html">Spring AOP Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-mvc-interview-questions-and-answers.html">Spring MVC Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-interview-questions-and-answers.html">Spring Security Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-rest-web-services-interview-questions-and-answers.html">Spring REST Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/04/spring-boot-interview-questions-and-answers.html">Spring Boot Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/04/spring-boot-microservices-interview-questions-answers.html">Spring Boot Microservices Interview Questions and Answers</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/dependency-injection-in-spring.html">Dependency Injection (DI) in Spring </a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/spring-ioc-container.html">Spring IoC Container</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/what-is-bean-factory-in-spring.html">What is Bean Factory in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/application-context-in-spring.html">ApplicationContext in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/bean-autowiring-in-spring.html">Bean Autowiring in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/understanding-bean-scopes.html">Spring Bean Scopes</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/create-custom-bean-scope-in-spring-example.html">Create Custom Bean Scope in Spring Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/06/using-applicationcontextaware-in-spring.html">Using ApplicationContextAware in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/bean-lifecycle-and-callbacks.html">Spring Bean Life Cycle and Callbacks</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/writing-beanpostprocessor-in-spring.html">BeanPostProcessor in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/writing-beanfactorypostprocessor-in.html">BeanFactoryPostProcessor in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/annotations-in-spring-and-based.html"> Annotations in Spring and Based Configuration</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/spring-jsr-250-annotations.html">Spring JSR-250 Annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/jsr-330-annotations-in-spring.html">JSR 330 Annotations in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Spring @Component, @Repository, @Service and @Controller Stereotype Annotations</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/method-injection-with-spring-using-lookup-method-property.html">Method injection with Spring using Lookup method property</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/introduction-to-aop-in-spring.html">Spring AOP-Introduction to Aspect Oriented Programming</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/07/spring-aspect-annotation.html">@Aspect Annotation in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-before-annotation-advice-example.html"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-before-advice-example-using-xml-config.html"> Spring AOP Before Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-annotation-advice-example.html"> Spring AOP AspectJ @After Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-advice-example-using-xml-config.html"> Spring AOP After Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-returning-annotation-advice-example.html"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-returning-advice-example-using-xml-config.html"> Spring AOP After-Returning Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-after-throwing-annotation-advice-example.html"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-after-throwing-advice-example-using-xml-config.html"> Spring AOP After Throwing Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-aspectj-around-annotation-advice-example.html"> Spring AOP AspectJ @Around Annotation Advice Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-around-advice-example-using-xml-config.html"> Spring AOP Around Advice Example using XML Config</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/understanding-aop-proxies-chapter-31.html">Spring AOP Proxies in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/spring-aop-transaction-management-in.html"> Spring AOP Transaction Management in Hibernate</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/transaction-management-in-spring.html">Spring Transaction Management</a></b></li>
<li><b><a href="https://dineshonjava.com/2012/12/declarative-transaction-management.html">Spring Declarative Transaction Management Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/03/spring-aop-ordering-of-aspects-with-example.html">Spring AOP-Ordering of Aspects with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-java-based-configuration-with-example.html">Spring Security Java Based Configuration with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/02/spring-security-xml-namespace-configuration-example.html">Spring Security XML Namespace Configuration Example</a></b></li>
</ol>
</div>
<div style="text-align: center;"><b><;<; </b><a href="http://50.87.249.200/~dineshon/2012/07/annotations-in-spring-and-based/"><b></b></a><b><a href="https://dineshonjava.com/2012/07/spring-jsr-250-annotations.html">Some JSR-250 Annotations</a></b><b> |<a href="https://dineshonjava.com/2012/06/spring-30-baby-step-to-learn.html">index</a>| ;</b><b><a href="https://dineshonjava.com/2012/07/stereotype-annotations-in-spring.html">Spring @Component Annotation</a></b><b>>;>;</b></div>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-resource-annotation/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-component-annotation/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '651'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…