Spring Bean Life Cycle

Vaishali Kalra
3 min readFeb 5, 2021

--

The life cycle of any object means how it is born and how it lives throughout its life and how it dies. Spring bean life cycle means, how the objects created in beans are managed by spring?

In the spring, objects are generated in beans and are handled by a spring IOC (Control Inversion) container using the two interfaces Application context and Bean Factory. The Application Context and the BeanFactory are the two spring interfaces that contain several bean objects in the XML configuration file and instantiate objects when necessary. Once the container has started in spring they firstly instantiate the bean and then perform the dependency injections if there is any and do the internal processing like configuration and assembling of objects to return a final object.

The Bean Instantiation: Once the bean is loaded using the Spring Container, the XML bean will be loaded from the given classpath resource using the given below line of code.

ApplicationContext context=new ClassPathXmlApplicationContext(“ben_life_cycle_spring.xml”);

This ApplicationContext will load the spring container in your code and the bean objects generated in the configuration will be instantiated depending on the need.

The Bean Destruction: The bean will destroy on the call of a close method in the Main POJO file:

((ClassPathXmlApplicationContext)context).close();

When the close() method is called, all reserved resources of the bean setup will be de-allocated.

Once spring begins processing, the custom code can be added to the bean life cycle. You can apply custom code to the XML configuration file using init and destroy methods. Custom code can be introduced during bean initialization using the init() method for a particular purpose, such as calling custom business logic methods or setting up resource handles (socket, DB, files, etc.). In the same way, the custom code can be applied at the time of bean destruction by overriding the destroy() function.

Bean Life Cycle

Special note about the init() and destroy() methods signatures when you are doing XML configuration in your project:

Method Name: The init() and destroy() methods can have any name but please do remember that the name is given in XML configuration with init and destroy must be the same as the method you define in the POJO class.

Access Modifier: The init() and destroy() methods are defined in POJO can have any access modifier public, private, or protected.

Return Type: The method can have any return type. However, “void’ is most commonly used. If you give a return type just note that you will not be able to capture the return value. As a result, “void” is commonly used.

Argument Type: These methods can not accept any kind of input, so they must be no-argument methods.

Bean Scope: Initialization lifecycle init() method can be called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle methods are not called. As in the scope bean prototype, several instances will be generated and will not be able to decide which bean object must be named to be closed.

The complete code can be found on the given Github link:

https://github.com/vaishaliarya/Spring/tree/main/Bean_lifecycle

--

--