0% found this document useful (0 votes)
75 views3 pages

SpringBoot BeanLife Cycle

The Spring Bean lifecycle consists of several phases including instantiation, dependency injection, post-processing, initialization, readiness for use, and destruction. Developers can customize these phases using annotations like @PostConstruct and @PreDestroy, as well as interfaces such as InitializingBean and DisposableBean. Understanding this lifecycle is crucial for effectively managing Spring beans within an application context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views3 pages

SpringBoot BeanLife Cycle

The Spring Bean lifecycle consists of several phases including instantiation, dependency injection, post-processing, initialization, readiness for use, and destruction. Developers can customize these phases using annotations like @PostConstruct and @PreDestroy, as well as interfaces such as InitializingBean and DisposableBean. Understanding this lifecycle is crucial for effectively managing Spring beans within an application context.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

In Spring Boot (and Spring Framework), the bean lifecycle refers to the various

stages a Spring-managed bean goes through from creation to destruction.


Understanding the lifecycle helps developers customize the behavior of beans at
different points during their life.
Overview of Spring Bean Lifecycle
The Spring Bean lifecycle can be broken down into several phases:
1. Bean Instantiation:
○ Spring creates the bean using reflection. For example, if you define
a bean in a @Configuration class with @Bean, Spring will instantiate the bean when
the application context is initialized.
2. Dependency Injection:
○ After the bean is created, Spring injects dependencies into the bean,
using mechanisms such as @Autowired, constructor injection, or @Value. This ensures
that the bean has all of the required dependencies before it is used.
3. Post-Processing (BeanPostProcessors):
○ Spring allows developers to modify the bean before and after
initialization via BeanPostProcessor implementations. There are two methods to
intervene:
§ postProcessBeforeInitialization: Called before the
@PostConstruct and custom initialization methods.
§ postProcessAfterInitialization: Called after the @PostConstruct
and custom initialization methods.
4. Initialization:
○ After dependencies are injected, Spring will initialize the bean,
executing any initialization logic:
§ If the bean implements InitializingBean, the
afterPropertiesSet() method is called.
§ If the bean has a method annotated with @PostConstruct, this
method will be executed after the properties have been set and dependencies
injected.
○ You can also use custom @Bean initialization methods in the
configuration class or use the initMethod attribute to specify initialization
logic.
5. Ready for Use:
○ Once the initialization phase is complete, the bean is ready to be
used by the Spring container. It can be injected into other components, accessed,
and invoked throughout the application.
6. Destruction:
○ When the Spring context is closed (for example, when the application
shuts down or the bean is no longer in use), Spring will destroy the bean:
§ If the bean implements DisposableBean, the destroy() method is
called.
§ If the bean has a method annotated with @PreDestroy, it will be
invoked before the bean is destroyed.
○ You can also specify a custom destruction method using the
destroyMethod attribute in the @Bean annotation.
Detailed Lifecycle Phases
Here’s a more detailed explanation of each phase:
1. Instantiation
• Spring uses Java reflection to create an instance of the bean.
• The bean can be created in various ways, such as through constructor
injection or factory methods.
• The bean can be defined using:
○ @Bean in a @Configuration class
○ @Component, @Service, @Repository, @Controller, etc., to mark a class
as a Spring bean.
2. Dependency Injection
• Once the bean is instantiated, Spring injects any required dependencies
into the bean. This is done using the @Autowired annotation or constructor
injection.
• Spring uses autowiring to satisfy the dependencies of a bean.
3. Post-Processing (BeanPostProcessors)
• Spring allows for post-processing of beans through BeanPostProcessor
implementations.
• postProcessBeforeInitialization is called before any initialization logic
(e.g., @PostConstruct or InitializingBean).
• postProcessAfterInitialization is called after initialization logic is
completed.
4. Initialization
• Initialization Logic:
○ If the bean implements InitializingBean, the afterPropertiesSet()
method is invoked.
○ If the bean has a method annotated with @PostConstruct, that method
is invoked after the properties are set.
○ You can specify a custom initialization method in
@Bean(initMethod="initMethodName").
Example of using @PostConstruct:

java
Copy code
import javax.annotation.PostConstruct;
public class MyBean {
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}
}
5. Ready for Use
• Once the initialization is done, the bean is fully ready and can be used
within the Spring context.
• The bean can now be injected into other components, accessed through
Spring’s Dependency Injection mechanism.
6. Destruction
• When the Spring container is shut down or the bean is no longer needed, it
is destroyed.
• If the bean implements DisposableBean, Spring calls the destroy() method.
• If the bean has a method annotated with @PreDestroy, that method will be
invoked before the bean is destroyed.
Example of using @PreDestroy:

java
Copy code
import javax.annotation.PreDestroy;
public class MyBean {
@PreDestroy
public void cleanup() {
System.out.println("Bean destroyed");
}
}
Spring Bean Lifecycle Summary
1. Bean Creation (Instantiation): Spring instantiates the bean.
2. Dependency Injection: Spring injects the required dependencies into the
bean.
3. Bean Post Processing: BeanPostProcessors modify the bean before and after
initialization.
4. Initialization: Spring initializes the bean by invoking @PostConstruct or
InitializingBean.
5. Ready for Use: The bean is now available for use in the application.
6. Destruction: On application shutdown, the @PreDestroy method or
DisposableBean interface is called to clean up resources.
Customizing the Bean Lifecycle
You can customize different stages of the bean lifecycle:
• Initialization: Use @PostConstruct, InitializingBean, or custom initMethod.
• Destruction: Use @PreDestroy, DisposableBean, or custom destroyMethod.
By leveraging these phases and customizing them, Spring Boot allows a lot of
flexibility in controlling how beans are managed within the application context.

You might also like