DIGITAL GURU
Java DSA Portfolio

Spring Bean Creation & Lifecycle

Learn what happens behind the scenes from bean instantiation, dependency injection, @PostConstruct, to @PreDestroy destruction.

Anuj Kumar Singh Written by Anuj Kumar Singh (Lead Engineer, 13+ yrs exp) 5 min read Verified Spring Boot 3+ Guide

Real-World Analogy

Think of the **Spring Bean Lifecycle** like enrolling in a university: 1. Application filed (Instantiation), 2. Tuition paid & dorm assigned (Dependency Injection), 3. Orientation attended (`@PostConstruct`), 4. Studying (Active Use), and 5. Graduation ceremony (`@PreDestroy`).

Detailed Phases of Bean Lifecycle

The Spring IoC container manages a well-defined lifecycle for every managed bean.

Lifecycle Phases:

  1. Instantiation: Spring instantiates bean via constructor or factory method.
  2. Populate Properties: Dependencies are injected via field, setter, or constructor injection.
  3. Aware Interfaces: Calls `BeanNameAware`, `BeanFactoryAware`, `ApplicationContextAware`.
  4. Post-Process Before Initialization: `BeanPostProcessor.postProcessBeforeInitialization()` runs.
  5. Initialization: Executes `@PostConstruct` methods or `InitializingBean.afterPropertiesSet()`.
  6. Post-Process After Initialization: `BeanPostProcessor.postProcessAfterInitialization()` runs (AOP proxies created here!).
  7. Ready for Use: Bean remains active in ApplicationContext.
  8. Destruction: When container closes, `@PreDestroy` methods run for cleanup.

Production Code Example:

LifecycleBean.java
package com.anujsingh.digitalguru.component;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.stereotype.Component;

@Component
public class LifecycleBean {

    @PostConstruct
    public void init() {
        System.out.println("Bean initialized & ready!");
    }

    @PreDestroy
    public void cleanup() {
        System.out.println("Bean cleaning up resources before shutdown.");
    }
}

Key Architectural Concepts & Best Practices:

When working with Spring Bean Creation & Lifecycle in enterprise Spring Boot applications, keep these key architectural guidelines in mind:

  • Separation of Concerns: Maintain a strict boundary between HTTP endpoints, service logic, and database persistence layers.
  • Framework Conventions: Rely on Spring Boot auto-configuration defaults whenever possible, overriding settings only via application.yml or @Configuration classes when customized behavior is required.
  • Production Monitoring & Reliability: Ensure proper exception handling, thread-safety, and resource cleanup to prevent memory leaks and unexpected runtime downtime.
  • Developer Ergonomics: Write clean, self-documenting code with modern Java features (Records, Lambdas, Streams) to simplify code reviews and maintenance.

Summary Takeaway:

Mastering Spring Bean Creation & Lifecycle ensures that your Java & Spring Boot backend microservices remain maintainable, secure, and compliant with modern enterprise software engineering standards.

Lifecycle Tip

Use `@PostConstruct` for initialization tasks that require injected dependencies to already be present.