/** * Add beans that implement ApplicationListener as listeners. * Doesn't affect other listeners, which can be added without being beans. */ protected void registerListeners() { // Register statically specified listeners first. for (ApplicationListener<?> listener : getApplicationListeners()) { getApplicationEventMulticaster().addApplicationListener(listener); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let post-processors apply to them! String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); for (String listenerBeanName : listenerBeanNames) { getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); } // Publish early application events now that we finally have a multicaster... Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; this.earlyApplicationEvents = null; if (earlyEventsToProcess != null) { for (ApplicationEvent earlyEvent : earlyEventsToProcess) { getApplicationEventMulticaster().multicastEvent(earlyEvent); } } }
/** * Add beans that implement ApplicationListener as listeners. * Doesn't affect other listeners, which can be added without being beans. */ protected void registerListeners() { // Register statically specified listeners first. for (ApplicationListener<?> listener : getApplicationListeners()) { getApplicationEventMulticaster().addApplicationListener(listener); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let post-processors apply to them! String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); for (String listenerBeanName : listenerBeanNames) { getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); } // Publish early application events now that we finally have a multicaster... Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; this.earlyApplicationEvents = null; if (earlyEventsToProcess != null) { for (ApplicationEvent earlyEvent : earlyEventsToProcess) { getApplicationEventMulticaster().multicastEvent(earlyEvent); } } }
@Override public void failed(ConfigurableApplicationContext context, Throwable exception) { ApplicationFailedEvent event = new ApplicationFailedEvent(this.application, this.args, context, exception); if (context != null && context.isActive()) { // Listeners have been registered to the application context so we should // use it at this point if we can context.publishEvent(event); } else { // An inactive context may not have a multicaster so we use our multicaster to // call all of the context's listeners instead if (context instanceof AbstractApplicationContext) { for (ApplicationListener<?> listener : ((AbstractApplicationContext) context) .getApplicationListeners()) { this.initialMulticaster.addApplicationListener(listener); } } this.initialMulticaster.setErrorHandler(new LoggingErrorHandler()); this.initialMulticaster.multicastEvent(event); } }
private static boolean hasListeners(ConfigurableApplicationContext context) { if (context.getBeanNamesForType(ShutdownApplicationListener.class).length != 0) { return true; } if (context instanceof AbstractApplicationContext) { if (((AbstractApplicationContext) context).getApplicationListeners().stream() .anyMatch(l -> l instanceof ShutdownApplicationListener)) { return true; } } if ((DefaultListableBeanFactory) context.getBeanFactory() .getSingleton(SHUTDOWN_LISTENER) != null) { return true; } return false; }
private static boolean hasListeners(ConfigurableApplicationContext context) { if (context.getBeanNamesForType(ShutdownApplicationListener.class).length != 0) { return true; } if (context instanceof AbstractApplicationContext) { if (((AbstractApplicationContext) context).getApplicationListeners().stream() .anyMatch(l -> l instanceof ShutdownApplicationListener)) { return true; } } if ((DefaultListableBeanFactory) context.getBeanFactory() .getSingleton(SHUTDOWN_LISTENER) != null) { return true; } return false; }
/** * @param serverSpec * @param listenerType * @param <T> * @return */ private <T extends Object> T doGetListenerFromContext(ServerSpec serverSpec, Class<T> listenerType) { ServerExecutionConfiguration serverConfig = ServerExecutionRegistry.getConfiguration(serverSpec); if (serverConfig == null) { throw new IllegalArgumentException("Can't find server config for specification: " + serverSpec + "\n Check the correct contextPath, execution name and server are used in annotations."); } AbstractApplicationContext remoteContext = (AbstractApplicationContext) serverConfig.getSpringContext(); // check statically defined listeners T listener = null; for (ApplicationListener ls : remoteContext.getApplicationListeners()) { if (listenerType.isAssignableFrom(listener.getClass())) { listener = (T) ls; } } if (listener == null) { // check beans: listener = remoteContext.getBean(listenerType); if (listener == null) { throw new IllegalArgumentException(String.format("No listener of type [%s] was found in context.", listenerType.getName())); } } return listener; }
delegatedBeanApplicationContext = (AbstractApplicationContext) beanFactory; if (!delegatedBeanApplicationContext.getApplicationListeners().contains(this)) { delegatedBeanApplicationContext.addApplicationListener(this);
/** * Add beans that implement ApplicationListener as listeners. * Doesn't affect other listeners, which can be added without being beans. */ protected void registerListeners() { // Register statically specified listeners first. for (ApplicationListener<?> listener : getApplicationListeners()) { getApplicationEventMulticaster().addApplicationListener(listener); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let post-processors apply to them! String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false); for (String listenerBeanName : listenerBeanNames) { getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName); } // Publish early application events now that we finally have a multicaster... Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents; this.earlyApplicationEvents = null; if (earlyEventsToProcess != null) { for (ApplicationEvent earlyEvent : earlyEventsToProcess) { getApplicationEventMulticaster().multicastEvent(earlyEvent); } } }
delegatedBeanApplicationContext = (AbstractApplicationContext) beanFactory; if (!delegatedBeanApplicationContext.getApplicationListeners().contains(this)) { delegatedBeanApplicationContext.addApplicationListener(this);