@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
- 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
- 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
- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Create the beans.xml file
- <?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
- 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
Download this project @PostConstruct-and-@PreDestroy.zip