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 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 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 Framework Annotations

Spring Framework Annotations Spring framework provides a variety of annotations for a better approach. Using annotations, we can develop Spring applications (small & large scale) easily with less code. Following are the annotations...

6 minutes read.

Spring – Beans using @Bean

Spring – Beans using @Bean We have previously discussed an example of Spring configuration using an XML-based configuration. In the previous example, we have defined beans inside the bean-configuration file (applicationContext.xml). To make the...

3 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 AOP – @AfterReturning Advice

Spring AOP – @AfterReturning Advice We have discussed earlier the types of advices available in Spring AOP. One of them is After Returning advice. It is an advice that executes after the normal execution...

3 minutes read.

Spring AOP – @After Advice

Spring AOP – @After Advice We have previously discussed the concepts of Spring AOP. One of the AOP concepts is Advice, which is further divided into various types. After (finally) is a type of advice...

3 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 - 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 – 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 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.

Spring Autowiring

Spring Autowiring Autowiring is an essential feature of the Spring framework. The autowiring function is used for wiring beans automatically, without injecting the beans using <constructor-arg> and <property> elements in the bean-config file. It can...

3 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 MVC Regular Expression Validation

Example of Regular Expression Validation Here, we are going to create an example of regular expression validation. Following are the steps to create an example of a regular expression: Create a request page In this step, we are going...

13 minutes read.

Spring AOP – @Around Advice

Spring AOP – @Around Advice We have discussed in our previous tutorial, the concepts, and terminology of the Spring AOP. Around advice is one of the types of advices. It can be applied to...

4 minutes read.

Spring MVC Form – Radio Button

A variety of form tags are available in Spring MVC for developing web applications, <form:radiobutton> tag is one of them. The radio button tag is used to choose only one option among a...

10 minutes read.

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...

4 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 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 – @Before Advice Type

Spring AOP – @Before Advice Type As we have discussed, AOP (Aspect-Oriented Programming) is one of the powerful features of the Spring framework. It complements the Object-Oriented Programming (OOPs). AOP provides some concepts and terminology,...

2 minutes read.