Spring Tutorial - Bean Life Cycle in Spring

Today in this chapter we will be discussing on the spring bean life cycle. Spring bean, when created it should perform some operations to come to a stable state. Similarly, when spring bean destroys it must perform some cleanup operation.

While defining the bean using <bean> attribute (as seen in Spring – Bean Definition ) we declare the <bean> with init-method and/or destroy-method. The init-method specifies the method that is to be called upon bean initialization. Whereas destroy-method specifies the method which will be called after the bean is destroyed.

1. Initialization Method

InitializingBean is the interface that we must implement if we want to perform some initialization work.

InitializingBean has only one method which is afterPropertiesSet() which can be implemented as follows:

public class InitializationBeanExample implements InitializingBean{
      public void afterPropertiesSet() throws Exception{
        // initialization work
      }
}

 

This is one way to declare the initialization method. Another way is an XML way. We can use init-method to declare the initialization method. This can be done in the following way:

<bean id=”initBean” class=”com. InitializationBeanExample” init-method=”init”/>

 

The class definition will be as follows:

public class InitializationBeanExample {
      public void init(){
          // initialization work
      }
}


The Spring BeanPostProcessor interface defines the callback method that we can implement to provide instantiation logic, dependency-resolution etc.

Defining the initialising method using annotation

To define the post initialisation method using annotation, we use @PostConstruct annotation. Example of this is given below:

2. Destruction method

DisposableBean is the interface that we must implement if we want to perform some destruction work. Destruction work can be like closing the connection or doing garbage collection.

DisposableBean interface has destroy() method which we need to implement. We can implement the interface in the following way:

public class DestructionBeanExample implements DisposableBean{
      public void destroy() throws Exception{
        // initialization work
      }
}

Like initialization the method is declared, destroy-method is the attribute that can be used to declare the destroy method.

<bean id=”destroyBean” class=”com.DestructionBeanExample” destroy-method=” destroy”/>

 

The class definition will be as follows:

public class DestructionBeanExample {
      public void destroy(){
          // initialization work
      }
}

 

Let us see the above discussion with an example. Here we will be creating three files HelloWorld.java, MainApp.java and Beans.xml.

The content of the files are as follows:

HelloWorld.java

MainApp.java

In the above example, we notice that we have used registerShoutdownHook() method that is declared on the AbstractApplicationContext class. This will ensure proper shutdown and will call the relevant destroy method.

Beans.xml

After creating the files compile and run the application.

Output

Bean is getting initialized

Your Message: Hello World

Bean is getting destroyed

Defining pre destroy method using annotation

To define the pre destroy method using annotation, we use @PreDestroy annotation. Example of this is given below:

Default Initialization and Destroy method

If there are too many beans for which we want to declare init and destroy method then we do not have to declare all of them. Instead, we can use default-init-method and default-destroy-method attributes in <bean> tag.

That’s all for this chapter. The next topic that we will discuss is Spring BeanPostProcessor

Post a Comment

0 Comments