Spring – Bean Lifecycle

Spring – Bean Lifecycle

Spring bean is responsible for managing the lifecycle of beans created by the Spring container.  The lifecycle of the bean is easy to understand. When a bean is created, it requires some initialization code to get into a usable state. Similarly, when the bean is no longer in use or removed from the Spring container, some cleanup code may be required.

To startup and shutdown a bean, we declare the init-method and the destroy-method attributes inside the <bean> tag in the bean-configuration file.

Init- method – It specifies a process which is called on the bean during the instantiation. We can give any name to the init-method.

Following code shows how to declare the init-method:

<bean id = "…" 
        class = "…"
        init-method = "doStartUp" >         

Destroy-method ­­­­­– It defines a process which is called on the bean before it is removed from the Spring container. Like the init- method, we can give any name to the destroy-method.

Following code shows how to declare the destroy-method:

<bean id = "…" 
        class ="…"
        init-method ="…"                               
        destroy-method ="doShutDown"> 

Example of Bean Lifecycle

Let’s understand the lifecycle of the Spring beans with the help of an example. In this example, we are going to create an interface Sim, which contains an unimplemented method calling(). A class Airtel_Sim implements the Sim interface and its unimplemented method.

Following are the steps to create this example:

Sim.java

public interface Sim {
             public String calling();
 } 

Airtel_Sim.java

public class Airtel_Sim implements Sim{
             public String calling() {   
                         return "Welcome to Airtel calling service..!";
             }
             // init method
             public void doStartUp(){
                         System.out.println("My Airtel Sim: Startup - The init method");
             }
             // destroy method 
             public void doShutDown(){
                                     System.out.println("My Airtel Sim: Shut Down - The destroy method");
             }
 } 

In the above Airtel_Sim class, we have initialized the init (doStartUp) and destroy (doShutDowm) methods. There is no particular name available for these two methods. We can give any name to the init and destroy methods.   

In the bean-config file (applicationContext), we need to add the entry of the init method and destroy method. So, we are going to add the entry inside the <bean> tag.

applicationContext.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"  
         xmlns:p = "http://www.springframework.org/schema/p"  
         xsi:schemaLocation = "http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <!-- Define the dependency  -->
  <bean id = "mysim" 
        class = "com.app.Bean_Lifecycle.Airtel_Sim"
        init-method = "doStartUp" 
        destroy-method = "doShutDown">
 </bean>
 </beans> 

App.java

import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class App 
 {
     public static void main( String[] args )
     {
         ClassPathXmlApplicationContext appcon = new    ClassPathXmlApplicationContext("applicationContext.xml");
         Sim sim = appcon.getBean("mysim", Sim.class); 
         System.out.println(sim.calling());
         appcon.close();
     }
 } 

Output

Spring container does not manage the prototype bean completely

The Spring does not call the destroy method for the prototype scope. In contrast to the other bean scopes, the Spring container does not manage the prototype bean completely.


Related Topics

Spring - Bean Scopes with Annotations

Spring - Bean Scopes with Annotations We have previously discussed the Spring bean scopes using an XML-based configuration. In the XML-based configuration, we have defined the beans and its scope inside the bean-config...

3 minutes read.

Spring – Dependency Injection

Spring – Dependency Injection One of the main features of Inversion of Control (IoC) is Dependency Injection (DI). It is a process of supplying the dependencies from one object to others. The Spring-Core container...

8 minutes read.

Spring MVC Form Checkbox

Spring MVC provides several form tags used to develop web applications (Usually JSP pages). One of them is a checkbox tag. The checkbox tag offers us to choose multiple options at the same...

12 minutes read.

Spring Autowiring – Setter Injection

Spring Autowiring – Setter Injection We have previously discussed an example of Setter Dependency Injection (SDI) using an XML file. In the previous case, we have defined the property name for the dependency...

2 minutes read.

Spring MVC Form – Drop-Down List

We have already discussed the Spring MVC form tags available in the form tag library. Form tags are used to develop web applications easily. In this tutorial, we are going to discuss the...

13 minutes read.

Spring - Bean lifecycle using annotations

Spring - Bean lifecycle using annotations We have already discussed the bean lifecycle using an XML-based configuration. In the previous case, we have declared the init-method and destroy-method attributes in the bean-configuration file. Here, we...

2 minutes read.

Spring Autowiring – Constructor Injection

Spring Autowiring – Constructor Injection We have previously discussed an example of Constructor Dependency Injection (CDI) using an XML-based configuration. In the previous case, we have defined the reference of the constructor in...

2 minutes read.

Spring Model Interface

Model Interface Model is an interface that defines a holder for model attributes. It is also used to transfer data between various parts of the Spring MVC application. The Model interface is available...

8 minutes read.

Spring MVC Example – Reading HTML Form Data

We have discussed earlier a simple example of Spring MVC in which we have created multiple view pages. Here, we are going to create another Spring MVC example in which we are...

7 minutes read.

Spring Autowiring –Field Injection

Spring Autowiring –Field Injection The Field Injection is used to inject the fields, variables, or properties of the beans. It is not available in autowiring using XML- based configuration. It is a...

2 minutes read.

Spring - Injecting values from Property Files

Spring - Injecting values from Property Files In this section, we will discuss how to inject values from a property file using the Java-based configuration. Here, we will use two annotations @Value and @PropertySource...

3 minutes read.

Spring MVC Form Validation

Spring Validation is used to validate the @Controller inputs. It is used to check and accept the user input in any web application. Using Spring validation, we can validate the user input on...

12 minutes read.

Spring - Inversion of Control (IoC)

Spring - Inversion of Control (IoC) The Inversion of Control (IoC) is a process where the objects define their dependencies, that is, the dependencies of other objects with they are working. It is done...

4 minutes read.

Spring – Annotation-based Inversion of Control (IoC)

Spring – Annotation-based Inversion of Control (IoC) We have previously discussed an example of Spring IoC (Inversion of Control). In the previous case, we have used the bean-configuration file to define the bean...

3 minutes read.

Spring AOP – Pointcut Expressions

Spring AOP – Pointcut Expressions Pointcut refers to a collection of join points that specify where advice to be applied. In other words, pointcut represents a set of various join points. It defines that...

7 minutes read.

Spring AOP - @AfterThrowing Advice

Spring AOP - @AfterThrowing Advice As we have discussed in our previous tutorial, Advice is one of the essential concepts of the Spring AOP. The after throwing advice is one of the types of...

3 minutes read.

Spring Tutorial

What is Spring Framework? Spring is one of the most popular enterprise application framework used for developing Java applications. “Spring is a lightweight open-source framework. It allows Java developer to build simple, reliable,...

6 minutes read.

Spring MVC Form Tag Library

The Spring MVC provides a variety of tags available in the form of a tag library, which is used to develop web pages (JSP view pages). The Spring’s form tag...

6 minutes read.

Spring - Bean Scope

Spring - Bean Scope Bean Scope refers to a lifecycle of the bean. It allows us to have control over the creation of bean objects. It is a very powerful approach and provides...

6 minutes read.

Spring MVC – @RequestParam

We have already discussed an example of Spring MVC in our previous tutorials. Here, we are going to discuss the @RequestParam annotation. In the last case, we had created a bean class for...

8 minutes read.