Skip to content

Commit ec470fb

Browse files
committed
Call Application and CommandLine Runners after ready event
Closes gh-7656
1 parent 27f550b commit ec470fb

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ public ConfigurableApplicationContext run(String... args) {
332332
new StartupInfoLogger(this.mainApplicationClass)
333333
.logStarted(getApplicationLog(), stopWatch);
334334
}
335+
callRunners(context, applicationArguments);
335336
return context;
336337
}
337338
catch (Throwable ex) {
@@ -757,7 +758,6 @@ protected void refresh(ApplicationContext applicationContext) {
757758
*/
758759
protected void afterRefresh(ConfigurableApplicationContext context,
759760
ApplicationArguments args) {
760-
callRunners(context, args);
761761
}
762762

763763
private void callRunners(ApplicationContext context, ApplicationArguments args) {

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

+45
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
import org.junit.Test;
3939
import org.junit.rules.ExpectedException;
4040
import org.mockito.ArgumentCaptor;
41+
import org.mockito.ArgumentMatchers;
42+
import org.mockito.InOrder;
43+
import org.mockito.Mockito;
4144
import reactor.core.publisher.Mono;
4245

4346
import org.springframework.beans.BeansException;
@@ -582,6 +585,33 @@ public void runCommandLineRunnersAndApplicationRunners() throws Exception {
582585
assertThat(this.context).has(runTestRunnerBean("runnerC"));
583586
}
584587

588+
@Test
589+
@SuppressWarnings("unchecked")
590+
public void runnersAreCalledAfterApplicationReadyEventIsPublished() throws Exception {
591+
SpringApplication application = new SpringApplication(
592+
MockRunnerConfiguration.class);
593+
application.setWebApplicationType(WebApplicationType.NONE);
594+
ApplicationListener<ApplicationReadyEvent> eventListener = mock(
595+
ApplicationListener.class);
596+
application.addListeners(eventListener);
597+
this.context = application.run();
598+
ApplicationRunner applicationRunner = this.context
599+
.getBean(ApplicationRunner.class);
600+
CommandLineRunner commandLineRunner = this.context
601+
.getBean(CommandLineRunner.class);
602+
InOrder applicationRunnerOrder = Mockito.inOrder(eventListener,
603+
applicationRunner);
604+
applicationRunnerOrder.verify(eventListener)
605+
.onApplicationEvent(ArgumentMatchers.any(ApplicationReadyEvent.class));
606+
applicationRunnerOrder.verify(applicationRunner)
607+
.run(ArgumentMatchers.any(ApplicationArguments.class));
608+
InOrder commandLineRunnerOrder = Mockito.inOrder(eventListener,
609+
commandLineRunner);
610+
commandLineRunnerOrder.verify(eventListener)
611+
.onApplicationEvent(ArgumentMatchers.any(ApplicationReadyEvent.class));
612+
commandLineRunnerOrder.verify(commandLineRunner).run();
613+
}
614+
585615
@Test
586616
public void loadSources() throws Exception {
587617
Class<?>[] sources = { ExampleConfig.class, TestCommandLineRunner.class };
@@ -1217,6 +1247,21 @@ public void fail() {
12171247

12181248
}
12191249

1250+
@Configuration
1251+
static class MockRunnerConfiguration {
1252+
1253+
@Bean
1254+
public CommandLineRunner commandLineRunner() {
1255+
return mock(CommandLineRunner.class);
1256+
}
1257+
1258+
@Bean
1259+
public ApplicationRunner applicationRunner() {
1260+
return mock(ApplicationRunner.class);
1261+
}
1262+
1263+
}
1264+
12201265
static class ExitStatusException extends RuntimeException
12211266
implements ExitCodeGenerator {
12221267

0 commit comments

Comments
 (0)