Lifecycle Hook
Ein Lifecycle Hook ist eine Methode, die der Container an festen Punkten im Lebenszyklus einer Bean aufruft.
Erkennbar an Annotationen wie @PostConstruct oder
@PreDestroy.
I. Wichtige Hooks
| Annotation | Wann läuft sie? |
|---|---|
| @PostConstruct | Nach Konstruktor + DI |
@PreDestroy |
Vor dem Zerstören (Pool-Shutdown, @Remove) |
@PrePersist |
Vor INSERT (@Entity) |
@PostPersist |
Nach INSERT |
@PreUpdate |
Vor UPDATE |
@PostLoad |
Nach SELECT |
II. Beispiel
@Stateless
public class HelloBean {
@PostConstruct
public void init() { /* Init-Logik */ }
@PreDestroy
public void cleanup() { /* Connection schließen */ }
}
III. .NET-Pendant
@PostConstruct≈ Konstruktor + DI ist fertig@PreDestroy≈Dispose()Pattern