@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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
    }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
    }
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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>
<?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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
 
    }
}
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

Karibasappa G C (KB)
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