Search in sources :

Example 1 with ApplicationListener

use of org.springframework.context.ApplicationListener in project spring-boot by spring-projects.

the class DelegatingApplicationListener method getListeners.

@SuppressWarnings("unchecked")
private List<ApplicationListener<ApplicationEvent>> getListeners(ConfigurableEnvironment environment) {
    if (environment == null) {
        return Collections.emptyList();
    }
    String classNames = environment.getProperty(PROPERTY_NAME);
    List<ApplicationListener<ApplicationEvent>> listeners = new ArrayList<>();
    if (StringUtils.hasLength(classNames)) {
        for (String className : StringUtils.commaDelimitedListToSet(classNames)) {
            try {
                Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
                Assert.isAssignable(ApplicationListener.class, clazz, "class [" + className + "] must implement ApplicationListener");
                listeners.add((ApplicationListener<ApplicationEvent>) BeanUtils.instantiateClass(clazz));
            } catch (Exception ex) {
                throw new ApplicationContextException("Failed to load context listener class [" + className + "]", ex);
            }
        }
    }
    AnnotationAwareOrderComparator.sort(listeners);
    return listeners;
}
Also used : ArrayList(java.util.ArrayList) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationEvent(org.springframework.context.ApplicationEvent) ApplicationContextException(org.springframework.context.ApplicationContextException) ApplicationContextException(org.springframework.context.ApplicationContextException)

Example 2 with ApplicationListener

use of org.springframework.context.ApplicationListener in project spring-boot by spring-projects.

the class LoggingApplicationListenerIntegrationTests method loggingPerformedDuringChildApplicationStartIsNotLost.

@Test
public void loggingPerformedDuringChildApplicationStartIsNotLost() {
    new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).child(Config.class).web(WebApplicationType.NONE).listeners(new ApplicationListener<ApplicationStartingEvent>() {

        private final Logger logger = LoggerFactory.getLogger(getClass());

        @Override
        public void onApplicationEvent(ApplicationStartingEvent event) {
            this.logger.info("Child application starting");
        }
    }).run();
    assertThat(this.outputCapture.toString()).contains("Child application starting");
}
Also used : SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 3 with ApplicationListener

use of org.springframework.context.ApplicationListener in project spring-framework by spring-projects.

the class AbstractApplicationEventMulticaster method retrieveApplicationListeners.

/**
	 * Actually retrieve the application listeners for the given event and source type.
	 * @param eventType the event type
	 * @param sourceType the event source type
	 * @param retriever the ListenerRetriever, if supposed to populate one (for caching purposes)
	 * @return the pre-filtered list of application listeners for the given event and source type
	 */
private Collection<ApplicationListener<?>> retrieveApplicationListeners(ResolvableType eventType, Class<?> sourceType, ListenerRetriever retriever) {
    LinkedList<ApplicationListener<?>> allListeners = new LinkedList<>();
    Set<ApplicationListener<?>> listeners;
    Set<String> listenerBeans;
    synchronized (this.retrievalMutex) {
        listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
        listenerBeans = new LinkedHashSet<>(this.defaultRetriever.applicationListenerBeans);
    }
    for (ApplicationListener<?> listener : listeners) {
        if (supportsEvent(listener, eventType, sourceType)) {
            if (retriever != null) {
                retriever.applicationListeners.add(listener);
            }
            allListeners.add(listener);
        }
    }
    if (!listenerBeans.isEmpty()) {
        BeanFactory beanFactory = getBeanFactory();
        for (String listenerBeanName : listenerBeans) {
            try {
                Class<?> listenerType = beanFactory.getType(listenerBeanName);
                if (listenerType == null || supportsEvent(listenerType, eventType)) {
                    ApplicationListener<?> listener = beanFactory.getBean(listenerBeanName, ApplicationListener.class);
                    if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
                        if (retriever != null) {
                            retriever.applicationListenerBeans.add(listenerBeanName);
                        }
                        allListeners.add(listener);
                    }
                }
            } catch (NoSuchBeanDefinitionException ex) {
            // Singleton listener instance (without backing bean definition) disappeared -
            // probably in the middle of the destruction phase
            }
        }
    }
    AnnotationAwareOrderComparator.sort(allListeners);
    return allListeners;
}
Also used : ApplicationListener(org.springframework.context.ApplicationListener) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) LinkedList(java.util.LinkedList)

Example 4 with ApplicationListener

use of org.springframework.context.ApplicationListener in project spring-framework by spring-projects.

the class ApplicationListenerDetector method postProcessBeforeDestruction.

@Override
public void postProcessBeforeDestruction(Object bean, String beanName) {
    if (this.applicationContext != null && bean instanceof ApplicationListener) {
        ApplicationEventMulticaster multicaster = this.applicationContext.getApplicationEventMulticaster();
        multicaster.removeApplicationListener((ApplicationListener<?>) bean);
        multicaster.removeApplicationListenerBean(beanName);
    }
}
Also used : ApplicationEventMulticaster(org.springframework.context.event.ApplicationEventMulticaster) ApplicationListener(org.springframework.context.ApplicationListener)

Example 5 with ApplicationListener

use of org.springframework.context.ApplicationListener in project spring-security by spring-projects.

the class InterceptMethodsBeanDefinitionDecoratorTests method targetDoesntLoseApplicationListenerInterface.

@Test
public void targetDoesntLoseApplicationListenerInterface() {
    assertThat(appContext.getBeansOfType(ApplicationListener.class)).hasSize(1);
    assertThat(appContext.getBeanNamesForType(ApplicationListener.class).length).isEqualTo(1);
    appContext.publishEvent(new AuthenticationSuccessEvent(new TestingAuthenticationToken("user", "")));
    assertThat(target instanceof ApplicationListener<?>).isTrue();
}
Also used : ApplicationListener(org.springframework.context.ApplicationListener) AuthenticationSuccessEvent(org.springframework.security.authentication.event.AuthenticationSuccessEvent) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken)

Aggregations

ApplicationListener (org.springframework.context.ApplicationListener)7 ArrayList (java.util.ArrayList)2 ApplicationEvent (org.springframework.context.ApplicationEvent)2 LinkedList (java.util.LinkedList)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ConfigurableBeanFactory (org.springframework.beans.factory.config.ConfigurableBeanFactory)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 AnsiOutputApplicationListener (org.springframework.boot.context.config.AnsiOutputApplicationListener)1 ConfigFileApplicationListener (org.springframework.boot.context.config.ConfigFileApplicationListener)1 ApplicationEnvironmentPreparedEvent (org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent)1 ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)1 ClasspathLoggingApplicationListener (org.springframework.boot.context.logging.ClasspathLoggingApplicationListener)1 LoggingApplicationListener (org.springframework.boot.context.logging.LoggingApplicationListener)1 ApplicationContextException (org.springframework.context.ApplicationContextException)1 ApplicationEventMulticaster (org.springframework.context.event.ApplicationEventMulticaster)1 SimpleApplicationEventMulticaster (org.springframework.context.event.SimpleApplicationEventMulticaster)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1