Object bean = entry.getValue();
//don't destroy the eventbus until the end - we need it to still be 'alive' while publishing destroy events:
if (bean != this.eventBus) { //memory equality check (not .equals) on purpose
LifecycleUtils.destroy(bean);
BeanEvent event = new DestroyedBeanEvent(id, bean, immutableObjects);
eventBus.publish(event);
this.eventBus.unregister(bean); //bean is now destroyed - it should not receive any other events
}
}
//only now destroy the event bus: