Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
In the Spring Framework, the lifecycle of a bean refers to the series of steps or events that a bean goes through from its instantiation until it is destroyed. Understanding the bean lifecycle is essential when working with Spring-based applications because it allows developers to tap into and customize various lifecycle events.
Here's an overview of the bean lifecycle in Spring:
Instantiation: The very first step in the lifecycle of a bean. The Spring container creates an instance of the bean.
Populate Properties: After instantiation, the Spring container injects the bean's properties using the dependency injection (DI) mechanism.
Set Bean Name: If the bean implements BeanNameAware
, the setBeanName
method is called with the bean ID.
Set Bean Factory: If the bean implements BeanFactoryAware
, the setBeanFactory
method is called, providing the bean with the instance of the bean factory that created it.
Pre-initialization - Bean Post Processors: Before a bean's initialization method is invoked, all BeanPostProcessors' postProcessBeforeInitialization
methods will be called.
AfterPropertiesSet and Custom Init Method:
InitializingBean
, the afterPropertiesSet
method is invoked.init-method
attribute in XML or @Bean(initMethod = "methodName")
in JavaConfig), that method is called.Post-initialization - Bean Post Processors: After a bean's initialization method is invoked, all BeanPostProcessors' postProcessAfterInitialization
methods will be called.
Bean is Ready to Use: At this point, the bean is fully constructed, initialized, and ready for use. It can now be retrieved from the container and used in the application.
DisposableBean and Custom Destroy Method:
DisposableBean
, the destroy
method is invoked.destroy-method
attribute in XML or @Bean(destroyMethod = "methodName")
in JavaConfig), that method is called.Bean is Destroyed: The bean is no longer available once it's destroyed.
To hook into the bean lifecycle, you can implement the various Aware interfaces (like BeanNameAware
, BeanFactoryAware
, etc.), use InitializingBean
and DisposableBean
, or specify custom init and destroy methods. Additionally, you can use @PostConstruct
and @PreDestroy
annotations from the JSR-250 specification to define methods that should be executed after bean construction and just before bean destruction, respectively.
Furthermore, BeanPostProcessor
can be implemented to perform operations before and after the bean initialization, giving developers a powerful mechanism to control the bean lifecycle and add custom behaviors during bean initialization.
Methods in Spring Bean Life Cycle:
afterPropertiesSet()
, init-method
, destroy()
, and destroy-method
.public class MyBean implements InitializingBean, DisposableBean { // Initialization method @Override public void afterPropertiesSet() throws Exception { // Initialization logic } // Destruction method @Override public void destroy() throws Exception { // Cleanup logic } }
Callback Methods in Spring Bean Life Cycle:
afterPropertiesSet()
and destroy()
provide hooks for custom initialization and destruction logic. Additionally, custom methods annotated with @PostConstruct
and @PreDestroy
serve as callback points.public class MyBean { // Initialization callback @PostConstruct public void customInit() { // Custom initialization logic } // Destruction callback @PreDestroy public void customDestroy() { // Custom destruction logic } }
Configuring Bean Life Cycle in Spring XML:
init-method
and destroy-method
attributes in the bean definition.<bean id="myBean" class="com.example.MyBean" init-method="customInit" destroy-method="customDestroy"/>
BeanPostProcessor in the Spring Bean Life Cycle:
BeanPostProcessor
is an interface that allows customization of bean instantiation and configuration. It provides hooks before and after the initialization of beans.public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // Custom logic before initialization return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // Custom logic after initialization return bean; } }
Working with ApplicationContextAware in Spring:
ApplicationContextAware
allows a bean to be aware of the application context. This enables access to the context and its resources during the bean life cycle.public class MyBean implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
Customizing Bean Life Cycle with BeanFactoryPostProcessor:
BeanFactoryPostProcessor
allows custom modification of the bean factory configuration before bean instantiation. It provides the ability to manipulate bean definitions.public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { // Custom logic to modify bean factory configuration } }
Using SmartLifecycle Interface in Spring for Custom Life Cycle Management:
SmartLifecycle
interface provides fine-grained control over the bean life cycle. It allows beans to manage their startup and shutdown phases based on specific conditions.public class MySmartLifecycleBean implements SmartLifecycle { @Override public void start() { // Custom logic for starting the bean } @Override public void stop() { // Custom logic for stopping the bean } @Override public boolean isRunning() { // Custom logic to determine if the bean is currently running return false; } }