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:
- public class Juggler implements Performer
- {
- private int _ballCount = 5;
- public Juggler() {}
- public Juggler(int ballCount)
- {
- _ballCount = ballCount;
- }
- public void Initialize()
- {
- System.out.println("Juggler: Registering in the show.");
- }
- public void Destroy()
- {
- System.out.println("Juggler: Signing off from the show.");
- }
- @Override
- public void Perform() throws PerformaceException
- {
- System.out.println("JUGGLING " + _ballCount + " BALLS");
- }
- }
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:
- <bean id="mike" class="spring.decoded.big.awards.Juggler" init-method="Initialize" destroy-method="Destroy">
- <constructor-arg value="10" />
- </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:
- public static void main(String[] args) throws PerformaceException
- {
- System.out.println("Starting the show....");
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/decoded/big/awards/big-awards.xml");
- Performer performer = (Performer) context.getBean("mike");
- if (!PrePerformaceChecks(performer, "Mike"))
- return;
- performer.Perform();
- System.out.println("\nMoving onto the next performance");
- performer = (Performer) context.getBean("john");
- if (!PrePerformaceChecks(performer, "John"))
- return;
- performer.Perform();
- System.out.println("\nMoving onto the next performance");
- performer = (Performer) context.getBean("shaun");
- if (!PrePerformaceChecks(performer, "Shaun"))
- return;
- performer.Perform();
- System.out.println("\nShow is over.");
- context.close();
- }
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:
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.