use of org.springframework.context.ApplicationEvent 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.ApplicationEvent in project spring-boot by spring-projects.
the class SpringApplicationTests method eventsArePublishedInExpectedOrder.
@Test
@SuppressWarnings("unchecked")
void eventsArePublishedInExpectedOrder() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class);
application.addListeners(listener);
this.context = application.run();
InOrder inOrder = Mockito.inOrder(listener);
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationStartingEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationEnvironmentPreparedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationContextInitializedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationPreparedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ContextRefreshedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationStartedEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(LivenessState.CORRECT)));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationReadyEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(ReadinessState.ACCEPTING_TRAFFIC)));
inOrder.verifyNoMoreInteractions();
}
use of org.springframework.context.ApplicationEvent in project spring-boot by spring-projects.
the class SpringApplicationTests method deregistersShutdownHookForFailedApplicationContext.
@Test
void deregistersShutdownHookForFailedApplicationContext() {
SpringApplication application = new SpringApplication(BrokenPostConstructConfig.class);
List<ApplicationEvent> events = new ArrayList<>();
application.addListeners(events::add);
application.setWebApplicationType(WebApplicationType.NONE);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(application::run);
assertThat(events).hasAtLeastOneElementOfType(ApplicationFailedEvent.class);
ApplicationFailedEvent failure = events.stream().filter((event) -> event instanceof ApplicationFailedEvent).map(ApplicationFailedEvent.class::cast).findFirst().get();
assertThat(SpringApplicationShutdownHookInstance.get()).didNotRegisterApplicationContext(failure.getApplicationContext());
}
use of org.springframework.context.ApplicationEvent in project spring-boot by spring-projects.
the class SpringApplicationTests method verifyRegisteredListenerSuccessEvents.
@SuppressWarnings("unchecked")
private void verifyRegisteredListenerSuccessEvents() {
ApplicationListener<ApplicationEvent> listener = this.context.getBean("testApplicationListener", ApplicationListener.class);
InOrder inOrder = Mockito.inOrder(listener);
then(listener).should(inOrder).onApplicationEvent(isA(ContextRefreshedEvent.class));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationStartedEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(LivenessState.CORRECT)));
then(listener).should(inOrder).onApplicationEvent(isA(ApplicationReadyEvent.class));
then(listener).should(inOrder).onApplicationEvent(argThat(isAvailabilityChangeEventWithState(ReadinessState.ACCEPTING_TRAFFIC)));
inOrder.verifyNoMoreInteractions();
}
use of org.springframework.context.ApplicationEvent in project spring-framework by spring-projects.
the class EventPublishingTestExecutionListenerTests method assertNoEvent.
private void assertNoEvent(Class<? extends TestContextEvent> eventClass, Consumer<TestContext> callback) {
callback.accept(testContext);
// The listener attempted to publish the event...
verify(testContext, times(1)).publishEvent(eventFactory.capture());
// But the event was not actually published since the ApplicationContext
// was not available.
verify(applicationContext, never()).publishEvent(any());
// In any case, we can still verify the type of event that would have
// been published.
ApplicationEvent event = eventFactory.getValue().apply(testContext);
assertThat(event).isInstanceOf(eventClass);
assertThat(event.getSource()).isEqualTo(testContext);
}
Aggregations