Skip to content

Commit 865203f

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-37937
2 parents 9c3b689 + 7d6532c commit 865203f

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
@@ -161,6 +161,7 @@
161161
* @author Brian Clozel
162162
* @author Ethan Rubinson
163163
* @author Chris Bono
164+
* @author Tadaya Tsuyukubo
164165
* @since 1.0.0
165166
* @see #run(Class, String[])
166167
* @see #run(Class[], String[])
@@ -741,18 +742,14 @@ protected void afterRefresh(ConfigurableApplicationContext context, ApplicationA
741742
}
742743

743744
private void callRunners(ApplicationContext context, ApplicationArguments args) {
744-
List<Object> runners = new ArrayList<>();
745-
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
746-
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
747-
AnnotationAwareOrderComparator.sort(runners);
748-
for (Object runner : new LinkedHashSet<>(runners)) {
745+
context.getBeanProvider(Runner.class).orderedStream().forEach((runner) -> {
749746
if (runner instanceof ApplicationRunner applicationRunner) {
750747
callRunner(applicationRunner, args);
751748
}
752749
if (runner instanceof CommandLineRunner commandLineRunner) {
753750
callRunner(commandLineRunner, args);
754751
}
755-
}
752+
});
756753
}
757754

758755
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;
@@ -157,6 +158,7 @@
157158
* @author Nguyen Bao Sach
158159
* @author Chris Bono
159160
* @author Sebastien Deleuze
161+
* @author Tadaya Tsuyukubo
160162
*/
161163
@ExtendWith(OutputCaptureExtension.class)
162164
class SpringApplicationTests {
@@ -627,6 +629,15 @@ void runCommandLineRunnersAndApplicationRunners() {
627629
assertThat(this.context).has(runTestRunnerBean("runnerC"));
628630
}
629631

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

16171628
}
16181629

1630+
@Configuration(proxyBeanMethods = false)
1631+
static class BeanDefinitionOrderRunnerConfig {
1632+
1633+
private final List<String> runners = new ArrayList<>();
1634+
1635+
@Bean
1636+
@Order
1637+
CommandLineRunner runnerC() {
1638+
return (args) -> this.runners.add("runnerC");
1639+
}
1640+
1641+
@Bean
1642+
@Order(Ordered.LOWEST_PRECEDENCE - 1)
1643+
ApplicationRunner runnerB() {
1644+
return (args) -> this.runners.add("runnerB");
1645+
}
1646+
1647+
@Bean
1648+
@Order(Ordered.HIGHEST_PRECEDENCE)
1649+
CommandLineRunner runnerA() {
1650+
return (args) -> this.runners.add("runnerA");
1651+
}
1652+
1653+
}
1654+
16191655
@Configuration(proxyBeanMethods = false)
16201656
static class ExitCodeCommandLineRunConfig {
16211657

0 commit comments

Comments
 (0)