Skip to content

Commit 7d6532c

Browse files
committed
Merge pull request #37905 from ttddyy
* gh-37905: Polish "Support @order on [CommandLine|Application]Runner @bean definitions" Support @order on [CommandLine|Application]Runner @bean definitions Closes gh-37905
2 parents 27f06ea + 04307aa commit 7d6532c

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
@@ -156,6 +156,7 @@
156156
* @author Brian Clozel
157157
* @author Ethan Rubinson
158158
* @author Chris Bono
159+
* @author Tadaya Tsuyukubo
159160
* @since 1.0.0
160161
* @see #run(Class, String[])
161162
* @see #run(Class[], String[])
@@ -740,18 +741,14 @@ protected void afterRefresh(ConfigurableApplicationContext context, ApplicationA
740741
}
741742

742743
private void callRunners(ApplicationContext context, ApplicationArguments args) {
743-
List<Object> runners = new ArrayList<>();
744-
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
745-
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
746-
AnnotationAwareOrderComparator.sort(runners);
747-
for (Object runner : new LinkedHashSet<>(runners)) {
744+
context.getBeanProvider(Runner.class).orderedStream().forEach((runner) -> {
748745
if (runner instanceof ApplicationRunner) {
749746
callRunner((ApplicationRunner) runner, args);
750747
}
751748
if (runner instanceof CommandLineRunner) {
752749
callRunner((CommandLineRunner) runner, args);
753750
}
754-
}
751+
});
755752
}
756753

757754
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
@@ -95,6 +95,7 @@
9595
import org.springframework.context.support.AbstractApplicationContext;
9696
import org.springframework.context.support.StaticApplicationContext;
9797
import org.springframework.core.Ordered;
98+
import org.springframework.core.annotation.Order;
9899
import org.springframework.core.env.CommandLinePropertySource;
99100
import org.springframework.core.env.CompositePropertySource;
100101
import org.springframework.core.env.ConfigurableEnvironment;
@@ -154,6 +155,7 @@
154155
* @author Nguyen Bao Sach
155156
* @author Chris Bono
156157
* @author Brian Clozel
158+
* @author Tadaya Tsuyukubo
157159
*/
158160
@ExtendWith(OutputCaptureExtension.class)
159161
class SpringApplicationTests {
@@ -667,6 +669,15 @@ void runCommandLineRunnersAndApplicationRunners() {
667669
assertThat(this.context).has(runTestRunnerBean("runnerC"));
668670
}
669671

672+
@Test
673+
void runCommandLineRunnersAndApplicationRunnersUsingOrderOnBeanDefinitions() {
674+
SpringApplication application = new SpringApplication(BeanDefinitionOrderRunnerConfig.class);
675+
application.setWebApplicationType(WebApplicationType.NONE);
676+
this.context = application.run("arg");
677+
BeanDefinitionOrderRunnerConfig config = this.context.getBean(BeanDefinitionOrderRunnerConfig.class);
678+
assertThat(config.runners).containsExactly("runnerA", "runnerB", "runnerC");
679+
}
680+
670681
@Test
671682
@SuppressWarnings("unchecked")
672683
void runnersAreCalledAfterStartedIsLoggedAndBeforeApplicationReadyEventIsPublished(CapturedOutput output)
@@ -1573,6 +1584,31 @@ TestCommandLineRunner runnerA() {
15731584

15741585
}
15751586

1587+
@Configuration(proxyBeanMethods = false)
1588+
static class BeanDefinitionOrderRunnerConfig {
1589+
1590+
private final List<String> runners = new ArrayList<>();
1591+
1592+
@Bean
1593+
@Order
1594+
CommandLineRunner runnerC() {
1595+
return (args) -> this.runners.add("runnerC");
1596+
}
1597+
1598+
@Bean
1599+
@Order(Ordered.LOWEST_PRECEDENCE - 1)
1600+
ApplicationRunner runnerB() {
1601+
return (args) -> this.runners.add("runnerB");
1602+
}
1603+
1604+
@Bean
1605+
@Order(Ordered.HIGHEST_PRECEDENCE)
1606+
CommandLineRunner runnerA() {
1607+
return (args) -> this.runners.add("runnerA");
1608+
}
1609+
1610+
}
1611+
15761612
@Configuration(proxyBeanMethods = false)
15771613
static class ExitCodeCommandLineRunConfig {
15781614

0 commit comments

Comments
 (0)