Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

"http://www.w3.org/TR/html4/loose.dtd">







InitializingBean and DisposableBean Interfaces


As an option to init-method and destroy-method, we could also rewrite the
Instrumentalist class to implement two special Spring interfaces: Initializing-
Bean and DisposableBean. The Spring container treats beans that implement
these interfaces in a special way by allowing them to hook into the bean lifecycle.


The InitializingBean interface mandates that the class implements afterPropertiesSet().
This method will be called once all specified properties for the

bean have been set. This makes it possible for the bean to perform initialization
that can’t be performed until the bean’s properties have been completely set. In
the Instrumentalist class, afterPropertiesSet() causes the Instrumentalist
to tune its instrument.

Similarly, DisposableBean requires that a destroy() method be implemented.
The destroy() method will be called on the other end of the bean’s lifecycle,
when the bean is disposed of by the container.


Example:-


package com.visualbuilder.beans;


import org.springframework.beans.factory.DisposableBean;

import org.springframework.beans.factory.InitializingBean;


public class DisposableInitBean implements InitializingBean, DisposableBean{


    public void afterPropertiesSet() throws Exception {

               
System.out.println("afterPropertiesSet method is called");

       
}


    public void destroy() throws Exception {

               
System.out.println("destroy method is called");

       
}


    }




DisposableInitExample.java



package com.visualbuilder.example;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.visualbuilder.beans.DisposableInitBean;


public class DisposableInitExample{


    public static void main (String [] ar){

           
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"com/visualbuilder/xml/spring-beans.xml"});

           
DisposableInitBean bean = (DisposableInitBean)context.getBean("disposableBean");

       
}

}






<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=
"http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">

   
<bean
id="disposableBean" class="com.visualbuilder.beans.DisposableInitBean" />


</beans>


 


Output:-


Jun 2, 2008 4:33:02 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]; startup date [Mon Jun 02 16:33:02 IST 2008]; root of context hierarchy

Jun 2, 2008 4:33:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [com/visualbuilder/xml/spring-beans.xml]

Jun 2, 2008 4:33:03 PM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory

INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c4bcdc]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2a5330

afterPropertiesSet method is called

Jun 2, 2008 4:33:03 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2a5330: defining beans [test,disposableBean,collectionExample,typeCollectionExample,constructorInjection,setterInjection,wrapperBean]; root of factory hierarchy


 




                    

Copyright © 2010 VisualBuilder. All rights reserved