DestructionAwareBeanPostProcessor class abstract

A BeanPostProcessor that is also called before each bean is destroyed.

Use it to release, in one place, something that many beans hold without each declaring a destroy callback: closing every sink, ending every subscription a framework added. It runs before the bean's own @PreDestroy and destroy callbacks. For one bean, a @PreDestroy method is simpler.

// Close every bean that is a Sink, when the context closes.
class ClosingPostProcessor extends DestructionAwareBeanPostProcessor {
  @override
  bool requiresDestruction(Object bean) => bean is Sink<Object?>;

  @override
  void postProcessBeforeDestruction(Object bean, String beanName) =>
      (bean as Sink<Object?>).close();
}

Spring (Java):

class ClosingPostProcessor implements DestructionAwareBeanPostProcessor {
  @Override
  public boolean requiresDestruction(Object bean) {
    return bean instanceof AutoCloseable;
  }
  @Override
  public void postProcessBeforeDestruction(Object bean, String name)
      throws Exception {
    ((AutoCloseable) bean).close();
  }
}
Inheritance

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

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
postProcessAfterInitialization(Object bean, String beanName) → Object
Called for each new bean after its init callbacks. The returned object (a proxy, say) is what the container exposes and injects. Also applied to FactoryBean products.
inherited
postProcessBeforeDestruction(Object bean, String beanName) → void
Called with the exposed bean (the one others received) before its @PreDestroy, DisposableBean.destroy and destroy-method callbacks run. Errors are logged and collected; they do not stop the shutdown.
postProcessBeforeInitialization(Object bean, String beanName) → Object
Called for each new bean after injection and the aware callbacks, before the init callbacks (@PostConstruct, afterPropertiesSet, init method). Returns the object to go on with, usually bean itself.
inherited
requiresDestruction(Object bean) → bool
Whether bean needs postProcessBeforeDestruction; true by default. Returning false lets the container skip tracking beans that have no destroy callbacks of their own.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited