Member-only story
Understanding Spring Bean Lifecycle
Master the Creation, Initialization, and Destruction Phases in Spring to Build Robust Java Applications

Introduction
The Spring Framework is key for Java enterprise development. It manages “beans,” which are objects, using a smart lifecycle system. It’s important for developers to grasp this lifecycle in Spring applications. This knowledge gives them control over how resources are set up, how they behave, and how to clean them up properly.
In this article, we’ll look at the Spring bean lifecycle. We’ll discuss how beans are created and destroyed. We’ll cover both theory and practical examples. In the end, you’ll grasp how Spring handles beans. You’ll learn how to use lifecycle hooks to boost your applications.
The Spring Bean lifecycle phases
The Spring bean lifecycle can be divided into several distinct phases:
- Bean definition loading
- Bean instantiation
- Populating properties
- BeanNameAware and BeanFactoryAware callbacks
- Pre-initialization
- InitializingBean and custom init methods
- Post-initialization (BeanPostProcessor)
- Bean is ready for use
- DisposableBean and custom destroy methods
Let’s examine each of these phases in detail.
1. Bean definition loading
Spring begins by loading bean definitions. It uses XML files, Java annotations or Java configuration clasess.
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserServiceImpl();
}
}
2. Bean instantiation
Spring creates an instance of the bean using one of several mechanisms:
public class UserServiceImpl implements UserService {
public UserServiceImpl() {
System.out.println("UserServiceImpl instantiated!");
}
}