Bean-Scopes und Lebenszyklusverwaltung
Lernen Sie, wie Spring Beans erstellt, mit Scopes versieht und zerstört und wie Sie sich mit Callbacks in ihren Lebenszyklus einklinken.
Bean-Scopes und Lebenszyklusverwaltung ist eine kostenlose Spring Boot 4 Complete Guide-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Complete Guide-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Complete Guide-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Lerne Java mit einem KI-Tutor — kostenlos
Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.
- Kurse
- 21
- Lektionen
- 84
Häufig gestellte Fragen
Ist die Lektion „Bean-Scopes und Lebenszyklusverwaltung“ kostenlos?
Ja — der vollständige Text von „Bean-Scopes und Lebenszyklusverwaltung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Complete Guide-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Complete Guide-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Bean-Scopes und Lebenszyklusverwaltung“?
Lernen Sie, wie Spring Beans erstellt, mit Scopes versieht und zerstört und wie Sie sich mit Callbacks in ihren Lebenszyklus einklinken. Du übst Spring Boot 4 Complete Guide mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Boot 4 Complete Guide zu starten?
Keine Vorkenntnisse erforderlich. Spring Boot 4 Complete Guide auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Bean-Scopes und Lebenszyklusverwaltung“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Boot 4 Complete Guide-Lektion Code schreiben und ausführen?
Ja. Jede Spring Boot 4 Complete Guide-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Den Spring-IoC-Container verstehen
- Dependency Injection in der Praxis
- Anwendungseigenschaften auslagern
- Bean-Scopes und Lebenszyklusverwaltung