A bean that wants a call when the container discards it, to release what it holds: connections, files, timers, subscriptions.
Singletons are destroyed when the context closes (dependents first);
custom-scoped beans when their scope ends. Prototypes are never
destroyed by the container. In application code @PreDestroy() on a
method does the same without an interface; this one suits framework
classes and hand-written definitions.
// The connection is closed when the application shuts down.
@Repository()
class Database implements DisposableBean {
final _connection = Connection.open();
@override
Future<void> destroy() => _connection.close(); // awaited by closeAsync()
}
Spring (Java):
@Repository
class Database implements DisposableBean {
private final Connection connection = Connection.open();
@Override public void destroy() { connection.close(); }
}
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
destroy(
) → FutureOr< void> -
Releases what the bean holds; runs after the
@PreDestroymethods and before the custom destroy method. What it throws is logged and collected, not rethrown, so the shutdown goes on. A returnedFutureis awaited bycloseAsync()(the synchronousclose()logs a warning and goes on). -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited