Spring Bean Lifecycle Interview Questions and Answers

Master Spring Bean Lifecycle with interview questions covering bean creation, initialization, dependency injection, @PostConstruct, InitializingBean, BeanPostProcessor, @PreDestroy, DisposableBean, lifecycle callbacks, and production best practices.


Introduction

Every object managed by the Spring IoC Container is called a Spring Bean.

A bean does not simply appear when requested. It passes through a well-defined lifecycle:

  1. Bean Definition
  2. Bean Instantiation
  3. Dependency Injection
  4. Aware Interfaces
  5. BeanPostProcessor (Before Initialization)
  6. Initialization
  7. BeanPostProcessor (After Initialization)
  8. Bean Ready for Use
  9. Bean Destruction

Understanding the bean lifecycle is one of the most frequently asked Spring interview topics because it explains how Spring creates, configures, initializes, and destroys objects.


Spring Bean Lifecycle

flowchart LR

BeanDefinition --> Instantiation

Instantiation --> DependencyInjection

DependencyInjection --> AwareInterfaces

AwareInterfaces --> BeforeInitialization

BeforeInitialization --> Initialization

Initialization --> AfterInitialization

AfterInitialization --> BeanReady

BeanReady --> Destruction

Q1. What is the Spring Bean Lifecycle?

Answer

The Spring Bean Lifecycle describes the complete sequence of events from bean creation until bean destruction.

Major phases

  • Bean creation
  • Dependency injection
  • Initialization
  • Ready for use
  • Destruction

Spring manages the entire lifecycle automatically through the IoC Container.


Q2. What happens during Bean Instantiation?

Instantiation is the process of creating the bean object.

Example

@Service

public class CustomerService {

}

Spring creates the object using:

  • Constructor
  • Factory Method
  • @Bean method

At this stage, dependencies have not yet been injected.


Q3. When are dependencies injected?

After bean creation, Spring injects all required dependencies.

Example

@Service

public class PaymentService {

    private final CustomerRepository repository;

    public PaymentService(

        CustomerRepository repository){

        this.repository = repository;

    }

}

Dependency Injection occurs before initialization.


Q4. What are Aware Interfaces?

Aware interfaces allow beans to access Spring infrastructure objects.

Common interfaces

  • BeanNameAware
  • BeanFactoryAware
  • ApplicationContextAware
  • EnvironmentAware

Example

public class DemoBean

implements

ApplicationContextAware{

}

Spring invokes these callbacks before initialization.


Q5. What is @PostConstruct?

@PostConstruct executes immediately after dependency injection.

Example

@PostConstruct

public void init(){

    System.out.println(

    "Bean Initialized");

}

Typical use cases

  • Cache loading
  • Resource initialization
  • Validation
  • Preloading configuration

Q6. What is InitializingBean?

Spring provides the InitializingBean interface.

Example

public class DemoBean

implements InitializingBean{

    @Override

    public void

    afterPropertiesSet(){

    }

}

afterPropertiesSet() executes after all dependencies have been injected.

Today, @PostConstruct is generally preferred.


Q7. What is BeanPostProcessor?

A BeanPostProcessor intercepts bean creation.

Methods

  • postProcessBeforeInitialization()
  • postProcessAfterInitialization()

BeanPostProcessor Flow

flowchart LR

Bean --> BeforeInitialization

BeforeInitialization --> Initialization

Initialization --> AfterInitialization

AfterInitialization --> Ready

Common uses

  • Logging
  • Proxy creation
  • AOP
  • Validation

Q8. What is @PreDestroy?

@PreDestroy executes before Spring destroys the bean.

Example

@PreDestroy

public void cleanup(){

}

Typical use cases

  • Close database connections
  • Release resources
  • Stop background threads
  • Flush caches

Q9. What is DisposableBean?

Spring provides another destruction callback.

Example

public class DemoBean

implements DisposableBean{

    @Override

   public void destroy(){

    }

}

Today,

@PreDestroy is generally preferred because it is standard Java/Jakarta.


Q10. Bean Lifecycle Best Practices

Prefer @PostConstruct

Instead of InitializingBean.


Prefer @PreDestroy

Instead of DisposableBean.


Keep Initialization Fast

Avoid long-running startup logic.


Release Resources Properly

Always close files, sockets, and database connections.


Avoid Heavy Business Logic

Lifecycle callbacks should focus on initialization and cleanup.


Banking Example

flowchart TD

SpringContainer --> CustomerService

CustomerService --> DependencyInjection

DependencyInjection --> PostConstruct

PostConstruct --> Ready

Ready --> PreDestroy

PreDestroy --> ResourcesReleased

The service initializes caches during startup and releases resources during shutdown.


Common Interview Questions

  • What is the Bean Lifecycle?
  • What happens during bean creation?
  • When does dependency injection occur?
  • What is @PostConstruct?
  • What is InitializingBean?
  • What is a BeanPostProcessor?
  • What is @PreDestroy?
  • What is DisposableBean?
  • Bean Lifecycle sequence?
  • Bean Lifecycle best practices?

Quick Revision

Topic Summary
Bean Instantiation Object creation
Dependency Injection Inject required beans
Aware Interfaces Access Spring infrastructure
@PostConstruct Initialization callback
InitializingBean Alternative initialization
BeanPostProcessor Intercept bean creation
Bean Ready Available for application
@PreDestroy Cleanup callback
DisposableBean Alternative destruction
Bean Lifecycle Complete bean management

Complete Bean Lifecycle

sequenceDiagram
Application->>Spring Container: Start
Spring Container->>Bean: Instantiate
Spring Container->>Bean: Inject Dependencies
Spring Container->>Bean: Call Aware Interfaces
Spring Container->>BeanPostProcessor: Before Initialization
BeanPostProcessor-->>Bean: Process
Spring Container->>Bean: @PostConstruct
Spring Container->>Bean: afterPropertiesSet()
Spring Container->>BeanPostProcessor: After Initialization
BeanPostProcessor-->>Bean: Process
Spring Container-->>Application: Bean Ready
Application->>Spring Container: Shutdown
Spring Container->>Bean: @PreDestroy
Spring Container->>Bean: destroy()

Production Example – Banking Customer Cache

A banking application maintains an in-memory cache of customer profiles.

Lifecycle

  • Spring creates the CustomerCacheService bean.
  • Dependencies such as CustomerRepository and RedisClient are injected.
  • @PostConstruct loads frequently accessed customer data into memory.
  • During application runtime, all requests use the initialized cache.
  • When the application shuts down, @PreDestroy flushes metrics, closes Redis connections, and releases allocated resources.
flowchart LR

SpringContainer --> CustomerCacheService

CustomerCacheService --> CustomerRepository

CustomerCacheService --> Redis

Redis --> CustomerCache

CustomerCacheService --> PostConstruct

PostConstruct --> LoadCache

CustomerCacheService --> PreDestroy

PreDestroy --> CloseResources

This approach improves startup consistency, ensures resources are released correctly, and prevents memory leaks in production environments.


Key Takeaways

  • The Spring Bean Lifecycle defines how beans are created, initialized, used, and destroyed by the IoC Container.
  • Bean creation consists of instantiation, dependency injection, Aware callbacks, BeanPostProcessor execution, and initialization.
  • @PostConstruct is the preferred way to execute initialization logic after dependencies have been injected.
  • BeanPostProcessor enables customization of bean creation and is widely used internally by Spring for AOP, proxies, and annotations.
  • @PreDestroy is the recommended mechanism for releasing resources before bean destruction.
  • Avoid placing heavy business logic inside lifecycle callbacks to keep application startup fast.
  • Proper lifecycle management helps prevent resource leaks, improves application stability, and simplifies maintenance.
  • Understanding the complete bean lifecycle is essential for debugging, customization, and advanced Spring Framework development.