Understanding the Lifecycle of Spring Beans: From Creation to Destruction
Written on
Chapter 1: Introduction to Spring Beans
Spring Beans are analogous to humans in their lifecycle. The Spring framework has emerged as the leading Java framework in the realm of backend development. For developers, comprehending the lifecycle of a Spring bean is essential to gain a deeper understanding and to tailor bean behavior effectively.
Section 1.1: Bean Instantiation
Beans are generated within Spring containers, similar to how humans come into existence. There are primarily two types of containers: BeanFactory and ApplicationContext.
In the case of a BeanFactory container, when a client requests a bean that has not yet been initialized or injects an uninitialized bean into another, the container will call createBean for instantiation, which is known as lazy loading. Conversely, an ApplicationContext container employs eager loading, instantiating all beans upon startup.
Section 1.2: Populating Attributes
Spring populates the attributes of beans by injecting dependencies based on the BeanDefinition.
- Inject Aware Interfaces: Spring checks if the object implements any Aware interfaces, injecting the corresponding instances into the bean.
- BeanPostProcessor: To customize logic prior to bean usage, implement the BeanPostProcessor interface, which offers two methods:
- postProcessBeforeInitialization(Object bean, String beanName)
- postProcessAfterInitialization(Object bean, String beanName)
Subsection 1.2.1: Initializing Beans
The InitializingBean interface provides a single method: afterPropertiesSet(). Additionally, you can configure an init-method within the bean to specify a method for execution.
Subsection 1.2.2: Destroying Beans
Similarly, DisposableBean and destroy-method can be utilized to designate a method for bean destruction.
To illustrate this, let’s create a personBean that implements several interfaces, including InitializingBean, BeanFactoryAware, BeanNameAware, and DisposableBean. During method execution, we will log messages indicating which method is being called.
This video titled "Life cycle of spring beans with bean creation process in detail" provides an in-depth look at how Spring manages bean creation.
Next, we will develop a customized BeanPostProcessor that implements postProcessBeforeInitialization and postProcessAfterInitialization, similarly logging messages.
In the video "A Bean's Life Cycle - Read Aloud #125," you can learn about the lifecycle of beans in a more engaging format.
Section 1.3: Configuring the XML File
We will configure an XML file to inform Spring about the bean definition and its required properties.
Finally, we will invoke an ApplicationContext container to create and subsequently destroy the bean.
Section 1.4: Observing the Result
The sequence of operations matches the initial diagram, progressing from instantiation to attribute population, through a series of initializations, and culminating in destruction.
I hope this article proves beneficial. As a backend software engineer, I encourage those interested in technology to follow my channel for updates inspired by my daily experiences in the field.
Read More:
An Important Theorem to Know for System Design Interviews
Batch Processing Using CompletableFuture and Blocking Queue Under High Concurrency Scenario
Get Connected:
My LinkedIn