0Pricing
Spring Boot 4 Complete Guide · Ders

Bean Kapsamları ve Yaşam Döngüsü Yönetimi

Spring'in bean'leri nasıl oluşturduğunu, kapsamlandırdığını ve yok ettiğini; geri çağırmalarla yaşam döngülerine nasıl bağlanacağınızı öğrenin.

Bean Kapsamları ve Yaşam Döngüsü Yönetimi, CoddyKit'te ücretsiz bir Spring Boot 4 Complete Guide dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Spring Boot 4 Complete Guide öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Complete Guide kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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.

Sıkça Sorulan Sorular

“Bean Kapsamları ve Yaşam Döngüsü Yönetimi” dersi ücretsiz mi?

Evet — “Bean Kapsamları ve Yaşam Döngüsü Yönetimi” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Spring Boot 4 Complete Guide kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Complete Guide kursu toplamda 4 dersten oluşur.

“Bean Kapsamları ve Yaşam Döngüsü Yönetimi” dersinde ne öğreneceğim?

Spring'in bean'leri nasıl oluşturduğunu, kapsamlandırdığını ve yok ettiğini; geri çağırmalarla yaşam döngülerine nasıl bağlanacağınızı öğrenin. Spring Boot 4 Complete Guide ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Spring Boot 4 Complete Guide öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Complete Guide, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Bean Kapsamları ve Yaşam Döngüsü Yönetimi” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Spring Boot 4 Complete Guide dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Spring Boot 4 Complete Guide dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Spring IoC Kapsayıcısını Anlamak
  2. Uygulamada Bağımlılık Enjeksiyonu
  3. Uygulama Özelliklerini Harici Hâle Getirme
  4. Bean Kapsamları ve Yaşam Döngüsü Yönetimi
← Spring Boot 4 Complete Guide Sayfasına Dön