Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring Bean Life Cycle: Initialization & Destruction

    • 0
    • 1
    • 1
    • 3
    • 0
    • 0
    • 0
    • 0
    • 559
    Comment on it

    Spring Bean Life Cycle has two core start and end points, namely Initialization and Destruction. When bean is instantiated it require set of actions which need to performed those are set into Initialization callbacks/method and one which are required just before removal of bean are set into Destruction callbacks/method.

    Initialization Callback:

    As mentioned above, if you want to execute some actions after instantiation of bean just put them in Initialization callback. There are two ways to achieve it:

    1. XML Base Configuration: In XML Base Configuration you can put your no argument method in init-method declaration. For example: Let say you defined one bean with id as myXMLBean as follows:

    <bean id=myXMLBean class=csa.MyXMLBean init-method=initMethod />
    

    On instantiation of myXMLBean initMethod will call automatically which is defined like show below in the MyXMLBean class.

    package csa;
    public class MyXMLBean{
           public void initMethod(){
                 //Initialization Code goes here
          }
    }
    

    2. Interface Implementation: Another way to make initialization work is to implement the single method from org.springframework.beans.factory.InitializingBean interface i.e.

    void afterPropertiesSet() throws Exception;
    

    For example:

    import org.springframework.beans.factory.*;
    package csa;
    public class MySimpleBean implements InitializingBean{
           public void afterPropertiesSet(){
                 //Initialization Code goes here
          }
    }
    

    Destruction Callback: As mentioned above, if you want to execute some actions before removal of bean just put them in Destruction callback. There are two ways to achieve it:

    1. XML Base Configuration: In XML Base Configuration you can put your no argument method in destroy-method declaration. For example: Let say you defined one bean with id as myXMLBean as follows:

    <bean id=myXMLBean class=csa.MyXMLBean destroy-method=destroyMethod />
    

    On destruction of myXMLBean destroyMethod will call automatically which is defined like show below in the MyXMLBean class.

    package csa;
    public class MyXMLBean{
           public void destroyMethod(){
                 //Destruction Code goes here
          }
    }
    

    2. Interface Implementation: Another way to make destruction work is to implement the single method from org.springframework.beans.factory.DisposableBean interface i.e.

    void destroy() throws Exception;
    

    For example:

    import org.springframework.beans.factory.*;
    package csa;
    public class MySimpleBean implements DisposableBean{
           public void destroy(){
                 //Destruction Code goes here
          }
    }
    

    Important: If let say you have lot of beans defined in XML configuration and you have individual init and destroy method for each then instead of specifying it again and again in each bean you can apply that to beans property as show below:

    <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-3.0.xsd"
        default-init-method="initMethod" 
        default-destroy-method="destroyMethod">
    
        <bean id="myXMLBean1" class="csa.MyXMLBean1" />
        <bean id="myXMLBean2" class="csa.MyXMLBean2" />
        .....
        <bean id="myXMLBeanN" class="csa.MyXMLBeanN" />
    </beans>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: