Skip to content

Commit 9219fdc

Browse files
committed
Merge branch '3.0.x' into 3.1.x
Closes gh-37938
2 parents 4b32f24 + 865203f commit 9219fdc

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@
3030
* @see CommandLineRunner
3131
*/
3232
@FunctionalInterface
33-
public interface ApplicationRunner {
33+
public interface ApplicationRunner extends Runner {
3434

3535
/**
3636
* Callback used to run the bean.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@
3333
* @see ApplicationRunner
3434
*/
3535
@FunctionalInterface
36-
public interface CommandLineRunner {
36+
public interface CommandLineRunner extends Runner {
3737

3838
/**
3939
* Callback used to run the bean.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
/**
20+
* Marker interface for runners.
21+
*
22+
* @author Tadaya Tsuyukubo
23+
* @see ApplicationRunner
24+
* @see CommandLineRunner
25+
*/
26+
interface Runner {
27+
28+
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
* @author Brian Clozel
164164
* @author Ethan Rubinson
165165
* @author Chris Bono
166+
* @author Tadaya Tsuyukubo
166167
* @since 1.0.0
167168
* @see #run(Class, String[])
168169
* @see #run(Class[], String[])
@@ -746,18 +747,14 @@ protected void afterRefresh(ConfigurableApplicationContext context, ApplicationA
746747
}
747748

748749
private void callRunners(ApplicationContext context, ApplicationArguments args) {
749-
List<Object> runners = new ArrayList<>();
750-
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
751-
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
752-
AnnotationAwareOrderComparator.sort(runners);
753-
for (Object runner : new LinkedHashSet<>(runners)) {
750+
context.getBeanProvider(Runner.class).orderedStream().forEach((runner) -> {
754751
if (runner instanceof ApplicationRunner applicationRunner) {
755752
callRunner(applicationRunner, args);
756753
}
757754
if (runner instanceof CommandLineRunner commandLineRunner) {
758755
callRunner(commandLineRunner, args);
759756
}
760-
}
757+
});
761758
}
762759

763760
private void callRunner(ApplicationRunner runner, ApplicationArguments args) {

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import org.springframework.context.support.AbstractApplicationContext;
9898
import org.springframework.context.support.StaticApplicationContext;
9999
import org.springframework.core.Ordered;
100+
import org.springframework.core.annotation.Order;
100101
import org.springframework.core.env.CommandLinePropertySource;
101102
import org.springframework.core.env.CompositePropertySource;
102103
import org.springframework.core.env.ConfigurableEnvironment;
@@ -158,6 +159,7 @@
158159
* @author Nguyen Bao Sach
159160
* @author Chris Bono
160161
* @author Sebastien Deleuze
162+
* @author Tadaya Tsuyukubo
161163
*/
162164
@ExtendWith(OutputCaptureExtension.class)
163165
class SpringApplicationTests {
@@ -628,6 +630,15 @@ void runCommandLineRunnersAndApplicationRunners() {
628630
assertThat(this.context).has(runTestRunnerBean("runnerC"));
629631
}
630632

633+
@Test
634+
void runCommandLineRunnersAndApplicationRunnersUsingOrderOnBeanDefinitions() {
635+
SpringApplication application = new SpringApplication(BeanDefinitionOrderRunnerConfig.class);
636+
application.setWebApplicationType(WebApplicationType.NONE);
637+
this.context = application.run("arg");
638+
BeanDefinitionOrderRunnerConfig config = this.context.getBean(BeanDefinitionOrderRunnerConfig.class);
639+
assertThat(config.runners).containsExactly("runnerA", "runnerB", "runnerC");
640+
}
641+
631642
@Test
632643
@SuppressWarnings("unchecked")
633644
void runnersAreCalledAfterStartedIsLoggedAndBeforeApplicationReadyEventIsPublished(CapturedOutput output)
@@ -1646,6 +1657,31 @@ TestCommandLineRunner runnerA() {
16461657

16471658
}
16481659

1660+
@Configuration(proxyBeanMethods = false)
1661+
static class BeanDefinitionOrderRunnerConfig {
1662+
1663+
private final List<String> runners = new ArrayList<>();
1664+
1665+
@Bean
1666+
@Order
1667+
CommandLineRunner runnerC() {
1668+
return (args) -> this.runners.add("runnerC");
1669+
}
1670+
1671+
@Bean
1672+
@Order(Ordered.LOWEST_PRECEDENCE - 1)
1673+
ApplicationRunner runnerB() {
1674+
return (args) -> this.runners.add("runnerB");
1675+
}
1676+
1677+
@Bean
1678+
@Order(Ordered.HIGHEST_PRECEDENCE)
1679+
CommandLineRunner runnerA() {
1680+
return (args) -> this.runners.add("runnerA");
1681+
}
1682+
1683+
}
1684+
16491685
@Configuration(proxyBeanMethods = false)
16501686
static class ExitCodeCommandLineRunConfig {
16511687

0 commit comments

Comments
 (0)