0Pricing
Spring Boot 4 Complete Guide · Lekcja

Zakresy beanów i zarządzanie cyklem życia

Nauczą się Państwo, jak Spring tworzy, określa zakres i usuwa beany oraz jak podłączać się do ich cyklu życia za pomocą callbacków.

Zakresy beanów i zarządzanie cyklem życia to bezpłatna lekcja Spring Boot 4 Complete Guide na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Complete Guide, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Complete Guide zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Zakresy beanów i zarządzanie cyklem życia” jest bezpłatna?

Tak — pełny tekst „Zakresy beanów i zarządzanie cyklem życia” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Complete Guide, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Complete Guide zawiera 4 lekcji w sumie.

Co nauczysz się w „Zakresy beanów i zarządzanie cyklem życia”?

Nauczą się Państwo, jak Spring tworzy, określa zakres i usuwa beany oraz jak podłączać się do ich cyklu życia za pomocą callbacków. Ćwiczysz Spring Boot 4 Complete Guide z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Complete Guide?

Nie wymagamy żadnego doświadczenia. Spring Boot 4 Complete Guide w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Zakresy beanów i zarządzanie cyklem życia”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Complete Guide?

Tak. Każda lekcja Spring Boot 4 Complete Guide zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Zrozumienie kontenera Spring IoC
  2. Wstrzykiwanie zależności w praktyce
  3. Eksternalizacja właściwości aplikacji
  4. Zakresy beanów i zarządzanie cyklem życia
← Powrót do Spring Boot 4 Complete Guide