0Pricing
Spring Boot 4 Microservices & REST APIs · Ders

Yaşam Döngüsü Geri Çağırmaları

Bean başlatma ve yok etme aşamalarına bağlanın.

Yaşam Döngüsü Geri Çağırmaları, CoddyKit'te ücretsiz bir Spring Boot 4 Microservices & REST APIs 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 Microservices & REST APIs öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.

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

The Bean Lifecycle

Beyond construction, beans pass through phases: the container instantiates them, injects dependencies, runs initialization callbacks, serves requests, then runs destruction callbacks at shutdown.

Hooking into these phases lets you set up and clean up resources reliably.

Why Not Use the Constructor?

A constructor runs before dependencies are injected when using setter/field injection, so collaborators may still be null. Initialization that depends on injected beans belongs in a post-construction callback, not the constructor.

With constructor injection the dependencies are present, but lifecycle callbacks still give a clear, intent-revealing place for setup.

@PostConstruct

A method annotated @PostConstruct runs once after the bean is fully constructed and all dependencies are injected. Use it for warm-up tasks like priming a cache or validating state.

@Service
public class CatalogService {
    private final ProductRepository repo;
    private List<Product> cache;
    public CatalogService(ProductRepository repo) { this.repo = repo; }

    @PostConstruct
    void warmUp() {
        this.cache = repo.findAllActive();
    }
}

@PreDestroy

A method annotated @PreDestroy runs once when the context is shutting down, before the bean is destroyed. Use it to release resources like connections, thread pools, or file handles.

@Service
public class ConnectionManager {
    private final ExecutorService pool = Executors.newFixedThreadPool(4);

    @PreDestroy
    void shutdown() {
        pool.shutdown();
    }
}

The InitializingBean Interface

Implementing InitializingBean provides afterPropertiesSet, called after dependencies are set. It works but couples your class to Spring’s API, so annotations are usually preferred.

@Service
public class IndexService implements InitializingBean {
    @Override
    public void afterPropertiesSet() {
        buildIndex();
    }
}

The DisposableBean Interface

Symmetrically, DisposableBean supplies a destroy method for cleanup. Like InitializingBean, it ties you to Spring interfaces.

@Service
public class IndexService implements DisposableBean {
    @Override
    public void destroy() {
        releaseIndex();
    }
}

@Bean initMethod and destroyMethod

For beans you create with @Bean (often third-party types), specify lifecycle methods by name. This keeps the external class free of Spring annotations.

@Bean(initMethod = "start", destroyMethod = "stop")
public CacheClient cacheClient() {
    return new CacheClient(config);
}

Ordering of Callbacks

When several mechanisms are present, the init order is: @PostConstruct first, then InitializingBean.afterPropertiesSet, then a custom initMethod. Destruction mirrors this with @PreDestroy first.

Preferred Approach

For modern code, prefer the JSR-250 annotations @PostConstruct and @PreDestroy. They are concise, framework-neutral, and clearly express intent without coupling to Spring interfaces.

A Caveat on Prototype Destruction

The container does not call destruction callbacks for prototype beans — it hands them off and forgets them. If a prototype holds resources, you are responsible for releasing them yourself.

Graceful Shutdown

Spring Boot triggers destruction callbacks during a clean shutdown (for example on SIGTERM). Combined with graceful shutdown settings, this lets in-flight work finish and resources close cleanly before exit.

server:
  shutdown: graceful
spring:
  lifecycle:
    timeout-per-shutdown-phase: 20s

Quick Check

Test your understanding of lifecycle callbacks.

Recap

Lifecycle callbacks manage setup and cleanup.

  • @PostConstruct runs after dependencies are injected
  • @PreDestroy runs before shutdown for cleanup
  • InitializingBean/DisposableBean work but couple to Spring
  • Use initMethod/destroyMethod for @Bean-created types
  • Prototype destruction is your responsibility

Sıkça Sorulan Sorular

“Yaşam Döngüsü Geri Çağırmaları” dersi ücretsiz mi?

Evet — “Yaşam Döngüsü Geri Çağırmaları” 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 Microservices & REST APIs kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Spring Boot 4 Microservices & REST APIs kursu toplamda 4 dersten oluşur.

“Yaşam Döngüsü Geri Çağırmaları” dersinde ne öğreneceğim?

Bean başlatma ve yok etme aşamalarına bağlanın. Spring Boot 4 Microservices & REST APIs 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 Microservices & REST APIs öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Spring Boot 4 Microservices & REST APIs, 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.

“Yaşam Döngüsü Geri Çağırmaları” 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 Microservices & REST APIs dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Spring Boot 4 Microservices & REST APIs 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 Container
  2. Yapıcı ve Alan Enjeksiyonu
  3. Bean Kapsamları
  4. Yaşam Döngüsü Geri Çağırmaları
← Spring Boot 4 Microservices & REST APIs Sayfasına Dön