Spring InitializingBean interface init bean annotation | Java @ Desk
Home » » Spring InitializingBean interface init bean annotation

Spring InitializingBean interface init bean annotation

Spring InitializingBean interface init bean 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) Override afterPropertiesSet - Belongs to org.springframework.beans.factory.InitializingBean package. Override the function to provide a callback function (afterPropertiesSet()) which the ApplicationContext will invoke when the bean is constructed
2) @PreDestroy - Belongs to javax.annotation.PreDestroy package

In case of annotation, Spring container will not be aware of @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>
2) SpringInitDestroy Bean
package com.spring;

import javax.annotation.PreDestroy;

import org.springframework.beans.factory.InitializingBean;

public class SpringInitDestroy implements InitializingBean {
 @PreDestroy
 public void cleanUp() throws Exception {
  System.out.println("Init method called on bean destroy");
 }

 @Override
 public void afterPropertiesSet() throws Exception {
  System.out
    .println("Init method called on bean initialization - afterPropertiesSet");
 }
}
3) SprintInitDestroyTest Client File
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 - afterPropertiesSet
Bean Created : com.spring.SpringInitDestroy@197bb7
Init method called on bean destroy





0 comments:

Post a Comment