Reading:  

Quick to the point introduction to Spring Framework


Bean Life Cycle

The bean factory of Spring is main responsible for the life cycle management of beans created via spring container. Bean required to do initialization to get it into a state of usable when a bean is instantiated and bean is removed from the container when the bean is no longer in use. The bean life cycle consist of call back methods which are divided into two types:

 

  • init-method: this attribute specifies a method which is needed to be called on a bean upon instantiation.
  • destroy-method: this is called before a bean removed from the container.

Initialization callbacks:

After necessary properties on bean are set by the container and  interface org.spring framework.beans.factory.Initializing Bean allows a bean to perform initialization task.

void afterPropertiesSet() throws Exception;

To implement the above interface and some initialization can be done inside afterPropertiesSet() method:

public class Student implements InitializingBean {
   public void afterPropertiesSet() {
      // user can do some initialization
   }
}

 

The metadata XML-based configuration, user can use init-method attribute to give name of the method that has a void no-argument signature. For example:

<bean id="Student" 
         class="examples.ExampleBean" init-method="ExmInit"/>
 

The class definition

public class Student {
   public void ExmInit() {
      // user can do some initialization
   }
}
 

Destruction callbacks:

The interface org.spring framework.beans.factory.DisposableBean specifies a single method:

void destroy() throws Exception;

To implement the above interface and some initialization can be done inside afterPropertiesSet() method:

public class Student implements InitializingBean {
   public void destroy() {
      // user can do some initialization
   }
}
 

The metadata XML-based configuration, user can use the destroy-method attribute to give the name of the method that has a void no-argument signature. For example:

<bean id="Student"
         class="examples.ExampleBean" destroy-method="destroy"/>

The class definition:

public class Student {
   public void destroy() {
      // user can do some initialization
   }
}

Example:

Java file: Customer

package pack1;  
  
public class Customer {  
private String cust_name;  
  
public String getCname() {  
    System.out.println("Customer Name: "+ cust_name);  
  }  
public void setCname(String cust_name) {  
    this.cust_name = cust_name;  
   }  
public void ExmInit(){
      System.out.println("In init block of Customer");
   }
   public void destroy(){
      System.out.println("In destroy block of Customer.");
   }
}  
 

Java file: ExmTest

package pack1;  
  
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
  
public class ExmTest {  
public static void main(String[] args) {  
AbstractApplicationContext
 cont = new ClassPathXmlApplicationContext("SingleBeans.xml");

    Customer firstCust=(Customer)cont.getBean("Customer");  
    firstCust.getCname();
  firstCust.registerShutdownHook();
             }  
}   

 

The configuration file is SingleBeans.xml:

<?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-3.0.xsd">
  
<bean id="Customer" class="pack1.Customer" 
      init-method="ExmInit" destroy-method="destroy">
       <property name="cust_name" value="Nimrit"/>
   </bean>
  </beans>  

Compile and run the program, we will get the results shown below:

Result:

In init block of Customer
Customer Name: Nimrit
In destroy block of Customer.

 

In the next part of this tutorial, we will take a quick look at Bean Post Processors

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!