use of io.micronaut.context.event.BeanDestroyedEvent in project micronaut-core by micronaut-projects.
the class DefaultBeanContext method disposeBean.
private <T> void disposeBean(BeanRegistration<T> registration, Argument<T> beanType, T finalBean, BeanDefinition<T> definition) {
final List<BeanPreDestroyEventListener> preDestroyEventListeners = resolveListeners(BeanPreDestroyEventListener.class, beanType);
T beanToDestroy = finalBean;
if (CollectionUtils.isNotEmpty(preDestroyEventListeners)) {
for (BeanPreDestroyEventListener<T> listener : preDestroyEventListeners) {
try {
final BeanPreDestroyEvent<T> event = new BeanPreDestroyEvent<>(this, definition, beanToDestroy);
beanToDestroy = Objects.requireNonNull(listener.onPreDestroy(event), "PreDestroy event listener illegally returned null: " + listener.getClass());
} catch (Exception e) {
throw new BeanDestructionException(definition, e);
}
}
}
try {
registration.close();
} catch (Exception e) {
throw new BeanDestructionException(definition, e);
}
final List<BeanDestroyedEventListener> postDestroyListeners = resolveListeners(BeanDestroyedEventListener.class, beanType);
if (CollectionUtils.isNotEmpty(postDestroyListeners)) {
for (BeanDestroyedEventListener<T> listener : postDestroyListeners) {
try {
final BeanDestroyedEvent<T> event = new BeanDestroyedEvent<>(this, definition, beanToDestroy);
listener.onDestroyed(event);
} catch (Exception e) {
throw new BeanDestructionException(definition, e);
}
}
}
}
Aggregations