@PostConstruct and @PreDestroy Annotations

These annotations are introduced in Spring 2.5 to call the bean life cycle methods just like init and destroy methods.

@PostConstruct : is called after the bean has been initialized and before this bean is returned to the requested object.

@PreDestroy : is called just before the bean is removed from the container

Create a PersonService class as below

  1. package com.kb.beans;
  2.  
  3. import javax.annotation.PostConstruct;
  4. import javax.annotation.PreDestroy;
  5.  
  6. public class PersonService {
  7.  
  8.     private Person person;
  9.  
  10.     public PersonService() {
  11.         System.out.println("PersonService no-arg constructor");
  12.     }
  13.  
  14.     @PostConstruct
  15.     public void Initialize() {
  16.         System.out.println("initializing the bean");
  17.     }
  18.  
  19.     @PreDestroy
  20.     public void cleanUp() {
  21.  
  22.         System.out.println("cleaning up the resources");
  23.     }
  24.  
  25.     public Person getPerson() {
  26.         return person;
  27.     }
  28.  
  29.     public void setPerson(Person person) {
  30.         this.person = person;
  31.     }
  32. }
package com.kb.beans;

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

public class PersonService {

	private Person person;

	public PersonService() {
		System.out.println("PersonService no-arg constructor");
	}

	@PostConstruct
	public void Initialize() {
		System.out.println("initializing the bean");
	}

	@PreDestroy
	public void cleanUp() {

		System.out.println("cleaning up the resources");
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
}

Create a Person class

  1. package com.kb.beans;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.config.BeanPostProcessor;
  5.  
  6. public class Person {
  7.    
  8.     private String name;
  9.    
  10.     public void setName(String name) {
  11.         this.name = name;
  12.     }
  13.  
  14.     public String getName() {
  15.         return name;
  16.     }
  17. }
package com.kb.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class Person {
	
	private String name;
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

Since we are using annotations, we need to add context:annotation-config element in spring bean configuration file to let the spring application know about these annotations.

Or we need to define the below bean in the spring config file

  1. <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

Create the beans.xml file

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:p="http://www.springframework.org/schema/p"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8.        http://www.springframework.org/schema/context
  9.         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  10.  
  11.     <context:annotation-config/>
  12.    
  13.     <bean id="person" class="com.kb.beans.Person">
  14.         <property name="name" value="Raj"/>
  15.     </bean>
  16.  
  17.    
  18.     <bean id="personService" class="com.kb.beans.PersonService">
  19.     <property name="person" ref="person"/>
  20.     </bean>
  21. </beans>
<?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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
  		http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:annotation-config/>
	
	<bean id="person" class="com.kb.beans.Person">
		<property name="name" value="Raj"/>
	</bean>

	
	<bean id="personService" class="com.kb.beans.PersonService">
	<property name="person" ref="person"/>
	</bean>
</beans>

Create the BeansLifeCycle class to see the output

  1. package com.kb.beans;
  2.  
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4.  
  5. public class BeansLifeCycle {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
  10.  
  11.         // Load PersonService object
  12.         PersonService personService = (PersonService) applicationContext.getBean("personService");
  13.         System.out.println("Name : " + personService.getPerson().getName());
  14.  
  15.         applicationContext.registerShutdownHook();
  16.  
  17.     }
  18. }
package com.kb.beans;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeansLifeCycle {

	public static void main(String[] args) {

		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

		// Load PersonService object
		PersonService personService = (PersonService) applicationContext.getBean("personService");
		System.out.println("Name : " + personService.getPerson().getName());

		applicationContext.registerShutdownHook();

	}
}

Run this class and see the output as below

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on