Spring Tutorial: Chapter 4

Introduction 

 
In this chapter we will look at another really useful feature from the Java Spring framework. There are occasions when you want to perform some initialization tasks in your class before the instance of it is ready to be consumed or used by anyone and also cleanup the resources of that class when done using it; as in:
  1. public class Juggler implements Performer   
  2. {  
  3.  private int _ballCount = 5;  
  4.  public Juggler() {}  
  5.  public Juggler(int ballCount)   
  6.  {  
  7.   _ballCount = ballCount;  
  8.  }  
  9.  public void Initialize()   
  10.  {  
  11.   System.out.println("Juggler: Registering in the show.");  
  12.  }  
  13.  public void Destroy()   
  14.  {  
  15.   System.out.println("Juggler: Signing off from the show.");  
  16.  }  
  17.  @Override  
  18.  public void Perform() throws PerformaceException   
  19.  {  
  20.   System.out.println("JUGGLING " + _ballCount + " BALLS");  
  21.  }  
  22. }  
So as you notice, before any performer in the show can perform, he needs to register into the show, so that is the first most important task that he should do before he can show his talent.
 
To do that, we need to use the Initialize() method; but wait, someone needs to invoke that method to make the Juggler eligible for the show. The first possibilty that occurs to us is to either invoke that method in our main method or have a Bootstrap class which does the job of invoking the initialize methods of all the beans.
  
Such a way would be very tedious and the list of such invokes would keep increasing and hence the maintainability of such code becomes very difficult.
  
Here comes Spring again to our rescue, wherein in my bean configuration file, I can ask Spring to perform initialization of my bean once the Spring context is created, as in:
  1. <bean id="mike" class="spring.decoded.big.awards.Juggler" init-method="Initialize" destroy-method="Destroy">  
  2.             <constructor-arg value="10" />  
  3. </bean>  
Notice that the init-method and destroy-method attributes specified with the bean declaration tells Spring to invoke the Initialize method of the bean once it is instantiated and destroy-method once the Spring context is closed or disposed of.
  
Let us see a working example of this. I have made changes to the PopularTalentShow::main(..) method and it should look something like this now: 
  1. public static void main(String[] args) throws PerformaceException   
  2. {  
  3.  System.out.println("Starting the show....");  
  4.  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/decoded/big/awards/big-awards.xml");  
  5.  Performer performer = (Performer) context.getBean("mike");  
  6.  if (!PrePerformaceChecks(performer, "Mike"))  
  7.   return;  
  8.  performer.Perform();  
  9.  System.out.println("\nMoving onto the next performance");  
  10.  performer = (Performer) context.getBean("john");  
  11.  if (!PrePerformaceChecks(performer, "John"))  
  12.   return;  
  13.  performer.Perform();  
  14.  System.out.println("\nMoving onto the next performance");  
  15.  performer = (Performer) context.getBean("shaun");  
  16.  if (!PrePerformaceChecks(performer, "Shaun"))  
  17.   return;  
  18.  performer.Perform();  
  19.  System.out.println("\nShow is over.");  
  20.  context.close();  
  21. }  
Notice the context.close() call which would invoke the destroy-method on all the beans specified in our bean declaration file and perform the cleanup task, now if you run the application:
 
 
Spring.jpg
 
 
Notice the initialization stuff happening in the beginning, and the cleanup being performed in the end, the hefty job being delegated to Spring with just little or no effort.