use of io.micronaut.context.event.BeanInitializingEvent in project micronaut-core by micronaut-projects.
the class AbstractBeanDefinition method postConstruct.
/**
* Default postConstruct hook that only invokes methods that require reflection. Generated subclasses should
* override to call methods that don't require reflection.
*
* @param resolutionContext The resolution hook
* @param context The context
* @param bean The bean
* @return The bean
*/
@SuppressWarnings({ "unused", "unchecked" })
@Internal
@UsedByGeneratedCode
protected Object postConstruct(BeanResolutionContext resolutionContext, BeanContext context, Object bean) {
boolean addInCreationHandling = isSingleton() && !CollectionUtils.isNotEmpty(postConstructMethods);
DefaultBeanContext.BeanKey key = null;
if (addInCreationHandling) {
// ensure registration as an inflight bean if a post construct is present
// this is to ensure that if the post construct method does anything funky to
// cause recreation of this bean then we don't have a circular problem
key = new DefaultBeanContext.BeanKey(this, resolutionContext.getCurrentQualifier());
resolutionContext.addInFlightBean(key, bean);
}
final Set<Map.Entry<Class, List<BeanInitializedEventListener>>> beanInitializedEventListeners = ((DefaultBeanContext) context).beanInitializedEventListeners;
if (CollectionUtils.isNotEmpty(beanInitializedEventListeners)) {
for (Map.Entry<Class, List<BeanInitializedEventListener>> entry : beanInitializedEventListeners) {
if (entry.getKey().isAssignableFrom(getBeanType())) {
for (BeanInitializedEventListener listener : entry.getValue()) {
bean = listener.onInitialized(new BeanInitializingEvent(context, this, bean));
if (bean == null) {
throw new BeanInstantiationException(resolutionContext, "Listener [" + listener + "] returned null from onInitialized event");
}
}
}
}
}
DefaultBeanContext defaultContext = (DefaultBeanContext) context;
for (int i = 0; i < methodInjectionPoints.size(); i++) {
MethodInjectionPoint methodInjectionPoint = methodInjectionPoints.get(i);
if (methodInjectionPoint.isPostConstructMethod() && methodInjectionPoint.requiresReflection()) {
injectBeanMethod(resolutionContext, defaultContext, i, bean);
}
}
if (bean instanceof LifeCycle) {
bean = ((LifeCycle) bean).start();
}
try {
return bean;
} finally {
if (addInCreationHandling) {
// ensure registration as an inflight bean if a post construct is present
// this is to ensure that if the post construct method does anything funky to
// cause recreation of this bean then we don't have a circular problem
resolutionContext.removeInFlightBean(key);
}
}
}
Aggregations