use of io.micronaut.context.event.BeanInitializedEventListener in project micronaut-core by micronaut-projects.
the class DefaultBeanContext method initializeEventListeners.
/**
* Initialize the event listeners.
*/
protected void initializeEventListeners() {
final Collection<BeanDefinition<BeanCreatedEventListener>> beanCreatedDefinitions = getBeanDefinitions(BeanCreatedEventListener.class);
final HashMap<Class, List<BeanCreatedEventListener>> beanCreatedListeners = new HashMap<>(beanCreatedDefinitions.size());
// noinspection ArraysAsListWithZeroOrOneArgument
beanCreatedListeners.put(AnnotationProcessor.class, Arrays.asList(new AnnotationProcessorListener()));
for (BeanDefinition<BeanCreatedEventListener> beanCreatedDefinition : beanCreatedDefinitions) {
try (BeanResolutionContext context = newResolutionContext(beanCreatedDefinition, null)) {
final BeanCreatedEventListener listener;
final Qualifier qualifier = beanCreatedDefinition.getDeclaredQualifier();
if (beanCreatedDefinition.isSingleton()) {
listener = createAndRegisterSingleton(context, beanCreatedDefinition, beanCreatedDefinition.getBeanType(), qualifier);
} else {
listener = doCreateBean(context, beanCreatedDefinition, Argument.of(BeanCreatedEventListener.class), qualifier);
}
List<Argument<?>> typeArguments = beanCreatedDefinition.getTypeArguments(BeanCreatedEventListener.class);
Argument<?> argument = CollectionUtils.last(typeArguments);
if (argument == null) {
argument = Argument.OBJECT_ARGUMENT;
}
beanCreatedListeners.computeIfAbsent(argument.getType(), aClass -> new ArrayList<>(10)).add(listener);
}
}
for (List<BeanCreatedEventListener> listenerList : beanCreatedListeners.values()) {
OrderUtil.sort(listenerList);
}
final HashMap<Class, List<BeanInitializedEventListener>> beanInitializedListeners = new HashMap<>(beanCreatedDefinitions.size());
final Collection<BeanDefinition<BeanInitializedEventListener>> beanInitializedDefinitions = getBeanDefinitions(BeanInitializedEventListener.class);
for (BeanDefinition<BeanInitializedEventListener> definition : beanInitializedDefinitions) {
try (BeanResolutionContext context = newResolutionContext(definition, null)) {
final Qualifier qualifier = definition.getDeclaredQualifier();
final BeanInitializedEventListener listener;
if (definition.isSingleton()) {
listener = createAndRegisterSingleton(context, definition, definition.getBeanType(), qualifier);
} else {
listener = doCreateBean(context, definition, Argument.of(BeanInitializedEventListener.class), qualifier);
}
List<Argument<?>> typeArguments = definition.getTypeArguments(BeanInitializedEventListener.class);
Argument<?> argument = CollectionUtils.last(typeArguments);
if (argument == null) {
argument = Argument.OBJECT_ARGUMENT;
}
beanInitializedListeners.computeIfAbsent(argument.getType(), aClass -> new ArrayList<>(10)).add(listener);
}
}
for (List<BeanInitializedEventListener> listenerList : beanInitializedListeners.values()) {
OrderUtil.sort(listenerList);
}
this.beanCreationEventListeners = beanCreatedListeners.entrySet();
this.beanInitializedEventListeners = beanInitializedListeners.entrySet();
}
use of io.micronaut.context.event.BeanInitializedEventListener 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