Spring @PostConstruct & @PreDestroy init bean method annotation
A bean configured in spring configuration file gets initialized when the file gets loaded in the spring container. There may arise a case where something needs to be initialized on bean initialization or needs to be destroyed before the bean nullifies.
Consider an example, the Properties file need to be loaded as soon as bean gets initialized. What happens to this property file when the bean gets destroyed. The best practice is to clear this property file. Also if the property file gets loaded on first client request, it would take additional time for the first request.
In order to resolve both, the property file gets loaded on bean initialization itself plus gets destroyed once the bean is destroyed using the following annotations :
1) @PostConstruct - Belongs to javax.annotation.PostConstruct package
2) @PreDestroy - Belongs to javax.annotation.PreDestroy package
In case of annotation, Spring container will not be aware of @PostConstruct and @PreDestroy annotation. To enable it, either of the following things need to be taken care of in apring configuration file:
Below is the sample implementation for this:
1) Spring Configuration file
2) SpringInitDestroy Bean
3) SprintInitDestroyTest Client File
Output :
A bean configured in spring configuration file gets initialized when the file gets loaded in the spring container. There may arise a case where something needs to be initialized on bean initialization or needs to be destroyed before the bean nullifies.
Consider an example, the Properties file need to be loaded as soon as bean gets initialized. What happens to this property file when the bean gets destroyed. The best practice is to clear this property file. Also if the property file gets loaded on first client request, it would take additional time for the first request.
In order to resolve both, the property file gets loaded on bean initialization itself plus gets destroyed once the bean is destroyed using the following annotations :
1) @PostConstruct - Belongs to javax.annotation.PostConstruct package
2) @PreDestroy - Belongs to javax.annotation.PreDestroy package
In case of annotation, Spring container will not be aware of @PostConstruct and @PreDestroy annotation. To enable it, either of the following things need to be taken care of in apring configuration file:
1) Specify the <context:annotation-config />
2) Specify the <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Below is the sample implementation for this:
1) Spring Configuration 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <bean id="springInitDestroy" class="com.spring.SpringInitDestroy"> </bean> </beans>
package com.spring; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class SpringInitDestroy { @PostConstruct public void initIt() throws Exception { System.out.println("Init method called on bean initialization"); } @PreDestroy public void cleanUp() throws Exception { System.out.println("Init method called on bean destroy"); } }
package com.spring; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SprintInitDestroyTest { public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "com//spring//appContext.xml" }); SpringInitDestroy initDestroy = (SpringInitDestroy) context .getBean("springInitDestroy"); System.out.println("Bean Created : " + initDestroy); context.close(); // @PreDestory method gets called here } }
Output :
Init method called on bean initialization
Bean Created : com.spring.SpringInitDestroy@1a897a9
Init method called on bean destroy
No comments:
Post a Comment