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;
}
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");
}
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;
}
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);
}
}
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();
}
Aggregations