0Pricing
Spring Boot 4 Complete Guide · Lesson

Bean Scopes and Lifecycle Management

Learn how Spring creates, scopes, and destroys beans, and how to hook into their lifecycle with callbacks.

Bean Scopes and Lifecycle Management is a free Spring Boot 4 Complete Guide lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Complete Guide learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Bean Scope?

A scope defines how many instances of a bean Spring creates and how long they live. The default scope is singleton: one shared instance per container.

Singleton Scope

With singleton, every injection point receives the same object. This is ideal for stateless services and is the most common choice.

@Service
public class PricingService {
    // one shared instance across the app
}

Prototype Scope

The prototype scope creates a new instance every time the bean is requested. Use it for stateful or short-lived objects.

@Component
@Scope("prototype")
public class ReportBuilder {
}

Web-Aware Scopes

In web apps you also have request and session scopes, tying a bean's lifetime to a single HTTP request or a user session respectively.

  • request — one per HTTP request
  • session — one per user session

Choosing the Right Scope

Default to singleton for stateless logic. Reach for other scopes only when a bean must hold per-request or per-instance state, since wider sharing of mutable state causes bugs.

The Bean Lifecycle

Spring instantiates a bean, injects its dependencies, runs initialization callbacks, serves it during the app's life, and finally runs destruction callbacks on shutdown.

Initialization with @PostConstruct

Annotate a method with @PostConstruct to run setup logic after dependencies are injected, such as opening a connection or warming a cache.

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

Cleanup with @PreDestroy

A @PreDestroy method runs just before the bean is removed, letting you release resources cleanly.

@PreDestroy
public void cleanup() {
    System.out.println("Releasing resources");
}

Lazy Initialization

By default singletons are created at startup. Marking a bean @Lazy defers creation until it is first needed, which can speed up boot time for rarely used beans.

@Lazy
@Service
public class HeavyService {
}

Prototype Beans and Destruction

Be aware that Spring does not manage the full destruction lifecycle of prototype beans. The container hands them off and forgets them, so you must release their resources yourself.

Putting It Together

A typical singleton service uses @PostConstruct to initialize and @PreDestroy to clean up, giving you precise control over startup and shutdown behavior.

@Service
public class CacheService {
    @PostConstruct void load() {}
    @PreDestroy void flush() {}
}

Quick Check

Test your understanding of bean scopes and lifecycle.

Recap

You learned the singleton, prototype, request, and session scopes, the bean lifecycle, and how to hook into it with @PostConstruct, @PreDestroy, and @Lazy.

Frequently asked questions

Is the “Bean Scopes and Lifecycle Management” lesson free?

Yes — the full text of “Bean Scopes and Lifecycle Management” is free to read here on the web, and the Spring Boot 4 Complete Guide course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Complete Guide course, upgrade to CoddyKit PRO.

What will I learn in “Bean Scopes and Lifecycle Management”?

Learn how Spring creates, scopes, and destroys beans, and how to hook into their lifecycle with callbacks. You practise Spring Boot 4 Complete Guide with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Spring Boot 4 Complete Guide?

No prior experience is required. Spring Boot 4 Complete Guide on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Bean Scopes and Lifecycle Management” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Spring Boot 4 Complete Guide lesson?

Yes. Every Spring Boot 4 Complete Guide lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding Spring IoC Container
  2. Dependency Injection in Practice
  3. Externalizing Application Properties
  4. Bean Scopes and Lifecycle Management
← Back to Spring Boot 4 Complete Guide