Member-only story

Understanding Spring Bean Lifecycle

Master the Creation, Initialization, and Destruction Phases in Spring to Build Robust Java Applications

Egor Voronianskii
7 min readFeb 28, 2025
Coffee beans, but today about different beans

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:

  1. Bean definition loading
  2. Bean instantiation
  3. Populating properties
  4. BeanNameAware and BeanFactoryAware callbacks
  5. Pre-initialization
  6. InitializingBean and custom init methods
  7. Post-initialization (BeanPostProcessor)
  8. Bean is ready for use
  9. 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!");
}
}

3. Populating properties

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

--

--

Egor Voronianskii
Egor Voronianskii

Written by Egor Voronianskii

I am a Software Engineer, JVM enthusiast, cat - lover, part-time surfer.

Responses (1)

Write a response