In this tutorial, we will explore how to use the InitializingBean
and
DisposableBean
interfaces in Spring for
bean lifecycle management. These interfaces provide hooks for performing actions during bean initialization
and destruction.
Spring beans have a lifecycle that includes phases such as instantiation, property setting, initialization,
and destruction. By implementing the InitializingBean
and DisposableBean
interfaces, we can hook into the
initialization and destruction phases to perform custom logic.
afterPropertiesSet()
method is called after the bean's
properties have been set.destroy()
method is called before the bean is destroyed.Create two classes MyInitializingBean
and MyDisposableBean
which will implement
InitializingBean
and DisposableBean
respectively.
package net.javaguides.spring.lifecycle;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class MyInitializingBean implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
// Custom initialization logic
System.out.println("MyInitializingBean: afterPropertiesSet method called");
}
}
package net.javaguides.spring.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;
@Component
public class MyDisposableBean implements DisposableBean {
@Override
public void destroy() throws Exception {
// Custom cleanup logic
System.out.println("MyDisposableBean: destroy method called");
}
}
Create a main class to run your Spring Boot application.
package net.javaguides.spring.lifecycle;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringLifecycleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringLifecycleApplication.class, args);
}
}
Run the SpringLifecycleApplication
class as a Java application. You should see the following
output in the console:
MyInitializingBean: afterPropertiesSet method called
When you shut down the application (e.g., by stopping the Spring Boot application in your IDE), you should see the following output in the console:
MyDisposableBean: destroy method called
InitializingBean
and overrides the
afterPropertiesSet
method to
include custom initialization logic. This method is called after the properties of the bean have been
set.
DisposableBean
and overrides the destroy
method to include custom
cleanup logic. This method is called when the Spring container is shutting down.@PostConstruct
and
@PreDestroy
annotations for initialization and destruction callbacks as they are more concise and easier to manage.
@PostConstruct
and @PreDestroy
).In this tutorial, we have learned how to use the InitializingBean
and
DisposableBean
interfaces in Spring for
managing bean lifecycle events. We have seen how to create a simple Spring Boot application to demonstrate
their usage and discussed best practices for their use.
This approach ensures that your beans are properly initialized and cleaned up, making your application more robust and maintainable.