Spring Bean Life Cycle

Buse Keklik
2 min readSep 25, 2023

--

Hello everyone, in this article I will explain the life cycle of spring bean.

In Java, the process of creating, using, and terminating an object is called “bean life cycle”. Bean life cycle is managed by spring container. A bean requires some initialization before it can be used, and similarly, it must be removed when it is no longer needed.

The Spring container is started when the program starts. This container manages and renders application components. When needed or in a pre-configured manner, the Spring container creates instances of beans, usually via constructor methods of bean classes. If the bean depends on other beans or resources, the Spring container injects these dependencies. This is done automatically based on the definitions and configurations of the dependencies. If there are special settings or initializations that need to be made ready for the Bean to be used, these settings are usually made through special markup mechanisms (@PostConstruct or init-method). Beans are used where needed within the application. When the program ends or the Spring container is closed, the termination (destroy) process of all beans in the Spring container is initiated. This may include operations such as freeing or clearing resources for beans. These last two steps can be customized with custom init-method and destroy-method definitions. For example, if you have custom code that needs to initialize or terminate a bean, you can define them by marking them as init-method and destroy-method.

@PostConstruct

It provides access to any implemented method immediately after an object is created and executes that method.

Example

import javax.annotation.PostConstruct;

public class MyBean {

private String message;

@PostConstruct
public void init() {
message = "Hello World";
System.out.println("Bean initialized and message set.");
}

public String getMessage() {
return message;
}
}

init() method

We will use init() method to execute all its code as the spring container starts up and the bean is instantiated.

Example

First, we need to configure the XML file and register the init() methods in this file.

<bean id="myBean" class="com.example.MyBean" init-method="customInit">
</bean>
package com.example;

public class MyBean {

public void customInit() {
System.out.println("The bean is initialized and the customInit method is called.");
}

}

We can put the initial operations to be performed when the bean is started in the customInit().

destroy() method

We will use destroy() method to execute all its code on closing the container.

Example

First, we need to configure the XML file and register the destroy() methods in this file.

<bean id="myBean" class="com.example.MyBean" init-method="customInit">
</bean>
package com.example;

public class MyBean {

public void customDestroy() {

System.out.println("The bean is terminated and the customDestroy method is called.");
}

}

We can put terminate operations to be performed when the bean is terminated in the customDestroy().

Thank you for reading. Enjoy your work😊.

--

--