Spring Framework Bean Life Cycle

SPONSOR AD

Spring Framework Bean Life Cycle

In this article, I am going to discuss Spring Framework Bean Life Cycle. Please read our previous article, where we discussed Spring Framework Beans. Let us do some hands-on with the life cycle of a bean.

What is a Bean’s Life Cycle?

The lifecycle of a Spring bean consists of several phases, from instantiation to destruction. Understanding this lifecycle is crucial for proper bean configuration and utilization.

  1. Instantiation: The Spring container creates instances of beans based on the configuration provided. This can be achieved through various mechanisms, including XML-based configurations, JavaConfig annotations, or component scanning.
  2. Dependency Injection: Once the bean instance is created, the Spring container injects the dependencies required by the bean. Dependency injection allows for loose coupling between components, enhancing flexibility, testability, and modularity.
  3. Initialization: After the dependencies are injected, the container initializes the bean. This phase involves invoking initialization call-backs or methods defined within the bean, allowing developers to perform necessary setup tasks.
  4. In Use: At this stage, the bean is ready for use within the application. Other components can now interact with the bean, utilizing its functionality.
  5. Destruction: When the application or the container shuts down, the Spring container triggers the destruction phase. During this phase, the container invokes destruction call-backs or methods defined in the bean, allowing for clean-up tasks, resource releases, or graceful shutdowns.
Implementing Bean Life Cycle in Spring Framework

In this program, we will be building upon the previous “Hello World” project from Article No. 3. Currently, the contents of the project are as follows:

App.java

Implementing Bean Life Cycle in Spring Framework

SPONSOR AD
Beans.xml

Implementing Bean Life Cycle in Spring Framework

HelloWorld.java

Implementing Bean Life Cycle in Spring Framework

Also, ensure that the required JAR files are in the project’s “Referenced Libraries” folder.

Spring Framework Bean Life Cycle

Step 1: Modify HelloWorld.java as follows:

Spring Framework Bean Life Cycle

We have added the init() and destroy() functions, which are responsible for initializing and destroying the bean respectively.

SPONSOR AD
Step 2: Modify App.java as follows:

Spring Framework Bean Life Cycle

We have performed the following modifications:

  1. Imported a new package.
  2. Added a new statement that will destroy the bean before the program finishes.
Step 3: Modify Beans.xml as follows:

Modify Beans.xml

We have specified the newly created methods as the initialization method and destruction method for the bean.

Step 4: Compile and execute the application. Ensure compilation is successful. Ensure that the output is as expected:

Complete Example Code of Spring Framework Bean Life Cycle

Congratulations! You have completed the bean life cycle application in Spring Framework!

Complete Example Code of Spring Framework Bean Life Cycle
App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App
{
    public static void main(String[] args)
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        HelloWorld hw = (HelloWorld) context.getBean("helloWorld");
        hw.getMsg();
        ((AbstractApplicationContext) context).registerShutdownHook();
    }
}
Beans.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"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "helloWorld" class = "HelloWorld" init-method = "init" destroy-method = "destroy">
      <property name = "msg" value = "Hello World!"/>
   </bean>

</beans>
HelloWorld.java
public class HelloWorld
{
    private String msg;

    public void getMsg()            {System.out.println(msg);}
    public void setMsg(String msg)  {this.msg = msg;}
    public void init()              {System.out.println("Bean is being initialised!");}
    public void destroy()           {System.out.println("Bean is being destroyed!");}
}

In the next article, I am going to discuss Spring Framework Bean Post Processors. Here, in this article, I try to explain Spring Framework Bean Life Cycle. I hope you enjoy this article.

SPONSOR AD

Leave a Reply

Your email address will not be published. Required fields are marked *