Clean • Professional
The Spring Bean Lifecycle explains how a bean is created, configured, managed, used, and destroyed by the Spring IoC (Inversion of Control) container.
Understanding this lifecycle helps you:
The Bean Lifecycle is the step-by-step process Spring follows to:
The Bean Lifecycle in Spring Boot is the sequence of steps a Spring-managed bean goes through from creation to destruction.
👉 You never manually create or destroy beans — Spring does it for you.
Knowing the bean lifecycle helps you:
BeanDefinitionLoaded
↓
BeanInstantiation
↓
DependencyInjection
↓
AwareInterfaces
↓
BeanPostProcessor(BeforeInit)
↓
@PostConstruct / InitializingBean / init-method
↓
BeanPostProcessor(AfterInit)
↓
BeanReadytoUse
↓
Lazy Beans (on-demand)
↓
ApplicationShutdown
↓
@PreDestroy / DisposableBean / destroy-method

Before Spring creates any object, it first reads and registers bean definitions (metadata) inside the IoC container.
Spring collects this information from:
@Component@Service@Repository@Controller@Configuration@Bean👉 What happens at this stage?
In this phase, Spring creates the actual bean object in memory.
Spring can instantiate a bean using:
@Bean methods)Example (Constructor-based instantiation):
@Component
publicclassUserService {
publicUserService() {
System.out.println("Bean instantiated");
}
}
👉 What happens at this stage?
After the bean object is created, Spring injects all required dependencies into it.
This process is called Dependency Injection (DI).
Spring supports three types of dependency injection:
Example (Field Injection):
@Autowired
private OrderService orderService;
👉 What happens at this stage?
If a Spring bean implements any Aware interface, Spring injects internal container information into that bean.
This allows the bean to interact directly with the Spring container.
Common Aware Interfaces
BeanNameAware → Provides the bean’s nameBeanFactoryAware → Provides the BeanFactoryApplicationContextAware → Provides the ApplicationContextEnvironmentAware → Provides environment and property detailsExample: ApplicationContextAware
@Component
publicclassMyBeanimplementsApplicationContextAware {
@Override
publicvoidsetApplicationContext(ApplicationContext context) {
System.out.println("ApplicationContext received");
}
}
👉 What happens here?
The @PostConstruct method runs after dependency injection is completed but before the bean is used by the application.
This phase is the best place to perform initialization logic that depends on injected beans.
Common Use Cases
Example
@Component
publicclassMyBean {
@PostConstruct
publicvoidinit() {
System.out.println("Bean initialized");
}
}
👉 What happens here?
Why @PostConstruct is Recommended
Three ways to perform initialization after dependencies are injected:
a) @PostConstruct (Recommended)
@Component
publicclassInitBean {
@PostConstruct
publicvoidinit() {
System.out.println("Bean initialized");
}
}
b) InitializingBean Interface
@Component
publicclassInitBeanimplementsInitializingBean {
@Override
publicvoidafterPropertiesSet() {
System.out.println("afterPropertiesSet called");
}
}
c) init-method in @Bean
@Bean(initMethod = "init")
public MyServicemyService() {
returnnewMyService();
}
After the bean is initialized, Spring applies BeanPostProcessor logic.
BeanPostProcessor allows Spring to modify or enhance beans before they are finally ready for use.
👉 You do not call this manually — Spring uses it internally.
What is it used for?
Spring relies on BeanPostProcessor to implement many powerful features:
@Transactional, @Async)@Secured, @PreAuthorize)✔ This is why annotations like @Transactional work
✔ This is a core mechanism behind Spring magic
At this stage, the Spring bean is fully prepared and available for use.
What has already happened:
✔ The bean is now managed completely by Spring
✔ It can be injected into controllers, services, schedulers, and other beans
✔ Application business logic starts executing here
@Lazy@Component
@Lazy
publicclassLazyBean {}
Useful for reducing startup time in large applications
This phase is triggered when the Spring application is shutting down.
It occurs when:
ApplicationContext is closedDuring this phase, Spring gives the bean a final chance to clean up resources.
Three ways to handle cleanup:
a) @PreDestroy (Recommended)
@Component
publicclassCleanupBean {
@PreDestroy
publicvoidcleanup() {
System.out.println("Releasing resources before shutdown");
}
}
b) DisposableBean Interface
@Component
publicclassCleanupBeanimplementsDisposableBean {
@Override
publicvoiddestroy() {
System.out.println("Cleanup done");
}
}
c) destroy-method in @Bean
@Bean(destroyMethod = "close")
public MyServicemyService() {
returnnewMyService();
}
Prototype beans: Spring does not manage destruction
@Component
publicclassLifecycleBean {
publicLifecycleBean() {
System.out.println("1. Constructor called");
}
@PostConstruct
publicvoidinit() {
System.out.println("2. @PostConstruct executed");
}
@PreDestroy
publicvoiddestroy() {
System.out.println("3. @PreDestroy executed");
}
}
Singleton beans
Prototype beans
Web-scoped beans
@PostConstruct and @PreDestroy