Skip to content

Commit d2b88d7

Browse files
author
Dave Syer
committed
Re-register SpringApplication listeners in context once it is available
Fixes gh-1141
1 parent b2db379 commit d2b88d7

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

spring-boot/src/main/java/org/springframework/boot/context/event/EventPublishingRunListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ private void registerApplicationEventMulticaster(
7777

7878
@Override
7979
public void contextLoaded(ConfigurableApplicationContext context) {
80+
for (ApplicationListener<?> listener : this.application.getListeners()) {
81+
context.addApplicationListener(listener);
82+
}
8083
publishEvent(new ApplicationPreparedEvent(this.application, this.args, context));
8184
}
8285

@@ -86,6 +89,8 @@ public void finished(ConfigurableApplicationContext context, Throwable exception
8689
publishEvent(new ApplicationFailedEvent(this.application, this.args, context,
8790
exception));
8891
}
92+
else {
93+
}
8994
}
9095

9196
private void publishEvent(SpringApplicationEvent event) {

spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Arrays;
2020
import java.util.Collections;
21+
import java.util.LinkedHashSet;
2122
import java.util.Set;
2223
import java.util.concurrent.atomic.AtomicReference;
2324

@@ -31,16 +32,19 @@
3132
import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
3233
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
3334
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
35+
import org.springframework.boot.context.event.ApplicationPreparedEvent;
3436
import org.springframework.context.ApplicationContext;
3537
import org.springframework.context.ApplicationContextAware;
3638
import org.springframework.context.ApplicationContextInitializer;
39+
import org.springframework.context.ApplicationEvent;
3740
import org.springframework.context.ApplicationListener;
3841
import org.springframework.context.ConfigurableApplicationContext;
3942
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4043
import org.springframework.context.annotation.AnnotationConfigUtils;
4144
import org.springframework.context.annotation.Bean;
4245
import org.springframework.context.annotation.Configuration;
4346
import org.springframework.context.event.ContextRefreshedEvent;
47+
import org.springframework.context.event.SimpleApplicationEventMulticaster;
4448
import org.springframework.context.support.StaticApplicationContext;
4549
import org.springframework.core.Ordered;
4650
import org.springframework.core.env.CommandLinePropertySource;
@@ -55,8 +59,10 @@
5559
import org.springframework.util.StringUtils;
5660

5761
import static org.hamcrest.Matchers.equalTo;
62+
import static org.hamcrest.Matchers.hasItem;
5863
import static org.hamcrest.Matchers.instanceOf;
5964
import static org.hamcrest.Matchers.is;
65+
import static org.hamcrest.Matchers.isA;
6066
import static org.hamcrest.Matchers.sameInstance;
6167
import static org.hamcrest.Matchers.startsWith;
6268
import static org.junit.Assert.assertArrayEquals;
@@ -436,6 +442,39 @@ public void registerShutdownHook() throws Exception {
436442
verify(applicationContext.getApplicationContext()).registerShutdownHook();
437443
}
438444

445+
@Test
446+
public void registerListener() throws Exception {
447+
SpringApplication application = new SpringApplication(ExampleConfig.class);
448+
application.setApplicationContextClass(SpyApplicationContext.class);
449+
final LinkedHashSet<ApplicationEvent> events = new LinkedHashSet<ApplicationEvent>();
450+
application.addListeners(new ApplicationListener<ApplicationEvent>() {
451+
@Override
452+
public void onApplicationEvent(ApplicationEvent event) {
453+
events.add(event);
454+
}
455+
});
456+
this.context = application.run();
457+
assertThat(events, hasItem(isA(ApplicationPreparedEvent.class)));
458+
assertThat(events, hasItem(isA(ContextRefreshedEvent.class)));
459+
}
460+
461+
@Test
462+
public void registerListenerWithCustomMulticaster() throws Exception {
463+
SpringApplication application = new SpringApplication(ExampleConfig.class,
464+
Multicaster.class);
465+
application.setApplicationContextClass(SpyApplicationContext.class);
466+
final LinkedHashSet<ApplicationEvent> events = new LinkedHashSet<ApplicationEvent>();
467+
application.addListeners(new ApplicationListener<ApplicationEvent>() {
468+
@Override
469+
public void onApplicationEvent(ApplicationEvent event) {
470+
events.add(event);
471+
}
472+
});
473+
this.context = application.run();
474+
assertThat(events, hasItem(isA(ApplicationPreparedEvent.class)));
475+
assertThat(events, hasItem(isA(ContextRefreshedEvent.class)));
476+
}
477+
439478
@Test
440479
public void registerShutdownHookOff() throws Exception {
441480
SpringApplication application = new SpringApplication(ExampleConfig.class);
@@ -551,6 +590,16 @@ static class ExampleConfig {
551590

552591
}
553592

593+
@Configuration
594+
static class Multicaster {
595+
596+
@Bean
597+
public SimpleApplicationEventMulticaster applicationEventMulticaster() {
598+
return new SimpleApplicationEventMulticaster();
599+
}
600+
601+
}
602+
554603
@Configuration
555604
static class ExampleWebConfig {
556605

0 commit comments

Comments
 (0)