Case 1: Using custom initialization and destruction methods

As we already discussed in the bean life cycle, the custom initialization and destruction methods will leverage the developer to write their own methods for initialization and destruction. As developers are not coupled with Spring API, they can take advantage of choosing their own method signature.

Let's take a look at how to hook these methods in order for them to be used by the Spring container step by step:

  1. Create a Java application as Ch02_Bean_Life_Cycle and add it to the JAR, which we did in the previous project.
  1. Create a Demo_Custom_Init class under the com.ch02.beans package, as shown in the following piece of code:
public class Demo_Custom_Init { 
  private String message; 
  private String name; 
 
  public Demo_Custom_Init() { 
    // TODO Auto-generated constructor stub 
    System.out.println("constructor gets called for  
    initializing data membersin Custom init"); 
    message="welcome!!!"; 
    name="no name"; 
  } 
 
  @Override 
  public String toString() { 
    // TODO Auto-generated method stub 
    return message+"	"+name; 
  } 
} 
  1. Add to the class a myInit() method with the following code to perform initialization. Here, we are shifting name to uppercase:
public void myInit() 
{ 
  name=name.toUpperCase(); 
  System.out.println("myInit() get called"); 
} 
  1. Create the beans_life cycle.xml file in classpath to configure the bean similar to the previous project (refer to beans_classpath.xml from Ch01_Container_Initizatization). If you are creating a new file from scratch, don't forget to add prolong information and the namespace configuration as discussed in Chapter 1, Spring at Glance under the heading XMLBeanFactory.
  1. Add the bean definition to it, which is as follows:
<bean id="obj" class="com.ch02.beans.Demo_Custom_Init"></bean> 
</beans> 

Each bean has to be configured within a <bean> tag.

A <bean> tag contains many attributes; we will need to configure a minimum of two of them, which are as follows:

    • id: This specifies the reference name by which the container recognizes whose object it is managing. The ID must be unique within the container. Naming the ID is similar to reference in Java application.
    • class: This specifies whose object container is creating and managing. The value of the class attribute must be a fully qualified class name, as we did in the preceding configuration.

The syntax to configure a bean definition in XML is shown as follows:

<bean id="id_to_use"
class="fully_qualified_class_name"></bean>

The XML configuration is equivalent to the Java Code, as follows:

Demo_Custom_Init obj= new 
com.ch02.beans.Demo_Custom_Init();
There are a few more attributes that developers can use in configuration. We will see them one by one according to the scenarios in the upcoming chapters.
  1. The configuration shown in step 5 is the very basic configuration, which does not provide any information to the container about how to initialize the property name. Let's modify the configuration by adding the init-method attribute to specify the method name that is to be invoked to initialize the property after instantiation. The modified code is shown as follows:
<bean id="obj" class="com.ch02.beans.Demo_Custom_Init" 
  init- method="myInit"> 
</bean> 
  1. As we had added the attribute, now we need to add the myInit() method in the bean class as follows:
public void myInit() 
{ 
  name=name.toUpperCase(); 
  System.out.println("myInit() get called"); 
} 
  1. We can release the resource the same way as we perform initialization. Let's release the memory with a custom destruct method; we will first need to add it to the code as follows:
public void destroy() 
{ 
  name=null; 
  System.out.println("destroy called"); 
} 
  1. Configure the destruct method in the bean configuration by specifying the destroy-method, as shown in the following lines of code:
<bean id="obj" class="com.ch02.beans.demo_Custom_Init" 
  init-method="myInit" destroy-method="destroy"> 
</bean> 
  1. Create Test_Demo_Custom_Init with the main function. Initialize the container as we did earlier in Chapter 1, Spring at Glance, and get the instance of Demo_Custom_Init using getBean(), as shown in the following code snippet:
public static void main(String[] args) { 
  // TODO Auto-generated method stub 
  ApplicationContext context=new 
   ClassPathXmlApplicationContext("beans_lifecycle.xml"); 
  Demo_Custom_Init 
  obj=(Demo_Custom_Init)context.getBean("obj"); 
   System.out.println(obj); 
} 
  1. The execution of this code gives the following output:

The output clearly shows the life cycle phases as construction, initialization, use, and then destruction of the bean.

Don't be surprised by the absence of the destroy called statement. We can use the following code to elegantly shut down the container:

((AbstractApplicationContext)context).registerShutdownHook(); 

In addition to the preceding line of the main function, even the destroy called statement will be seen as a console output as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.200.211