0Pricing
Spring Boot 4 Complete Guide · Pelajaran

Cakupan Bean dan Pengelolaan Siklus Hidup

Pelajari cara Spring membuat, menentukan cakupan, dan menghancurkan bean, serta cara menghubungkan fungsi panggilan balik ke siklus hidupnya.

Cakupan Bean dan Pengelolaan Siklus Hidup adalah pelajaran Spring Boot 4 Complete Guide gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Complete Guide, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Complete Guide mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

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.

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Cakupan Bean dan Pengelolaan Siklus Hidup” gratis?

Ya — teks lengkap “Cakupan Bean dan Pengelolaan Siklus Hidup” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Complete Guide, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Complete Guide mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Cakupan Bean dan Pengelolaan Siklus Hidup”?

Pelajari cara Spring membuat, menentukan cakupan, dan menghancurkan bean, serta cara menghubungkan fungsi panggilan balik ke siklus hidupnya. Kamu berlatih Spring Boot 4 Complete Guide dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Complete Guide?

Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Complete Guide di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “Cakupan Bean dan Pengelolaan Siklus Hidup” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Complete Guide ini?

Ya. Setiap pelajaran Spring Boot 4 Complete Guide menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Memahami Kontainer IoC Spring
  2. Injeksi Dependensi dalam Praktik
  3. Memisahkan Properti Aplikasi
  4. Cakupan Bean dan Pengelolaan Siklus Hidup
← Kembali ke Spring Boot 4 Complete Guide