ComponentsLifecycle
Manage Lifecycle
Run setup, cleanup, and processors around component creation.
Lifecycle hooks run around component creation and shutdown. Use them when a component needs runtime work that does not belong in the constructor.
Constructor:
func NewCache(client CacheClient) *Cache {
return &Cache{client: client}
}Lifecycle hook:
func (c *Cache) Init(ctx context.Context) error {
return c.client.Ping(ctx)
}Lifecycle roles
| Interface | Use it for |
|---|---|
component.Initializer | setup after dependencies are wired |
component.Disposable | cleanup during application shutdown |
component.BeforeInitProcessor | cross-cutting work before initialization |
component.AfterInitProcessor | cross-cutting work after initialization |
Keep constructors cheap and deterministic. Put runtime setup and cleanup into lifecycle hooks.
