Skip to content

Commit

Permalink
Merge branch '2.7.x' into 3.0.x
Browse files Browse the repository at this point in the history
Closes gh-37937
  • Loading branch information
wilkinsona committed Oct 18, 2023
2 parents 9c3b689 + 7d6532c commit 865203f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,7 @@
* @see CommandLineRunner
*/
@FunctionalInterface
public interface ApplicationRunner {
public interface ApplicationRunner extends Runner {

/**
* Callback used to run the bean.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,7 +33,7 @@
* @see ApplicationRunner
*/
@FunctionalInterface
public interface CommandLineRunner {
public interface CommandLineRunner extends Runner {

/**
* Callback used to run the bean.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot;

/**
* Marker interface for runners.
*
* @author Tadaya Tsuyukubo
* @see ApplicationRunner
* @see CommandLineRunner
*/
interface Runner {

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
* @author Brian Clozel
* @author Ethan Rubinson
* @author Chris Bono
* @author Tadaya Tsuyukubo
* @since 1.0.0
* @see #run(Class, String[])
* @see #run(Class[], String[])
Expand Down Expand Up @@ -741,18 +742,14 @@ protected void afterRefresh(ConfigurableApplicationContext context, ApplicationA
}

private void callRunners(ApplicationContext context, ApplicationArguments args) {
List<Object> runners = new ArrayList<>();
runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
AnnotationAwareOrderComparator.sort(runners);
for (Object runner : new LinkedHashSet<>(runners)) {
context.getBeanProvider(Runner.class).orderedStream().forEach((runner) -> {
if (runner instanceof ApplicationRunner applicationRunner) {
callRunner(applicationRunner, args);
}
if (runner instanceof CommandLineRunner commandLineRunner) {
callRunner(commandLineRunner, args);
}
}
});
}

private void callRunner(ApplicationRunner runner, ApplicationArguments args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
Expand Down Expand Up @@ -157,6 +158,7 @@
* @author Nguyen Bao Sach
* @author Chris Bono
* @author Sebastien Deleuze
* @author Tadaya Tsuyukubo
*/
@ExtendWith(OutputCaptureExtension.class)
class SpringApplicationTests {
Expand Down Expand Up @@ -627,6 +629,15 @@ void runCommandLineRunnersAndApplicationRunners() {
assertThat(this.context).has(runTestRunnerBean("runnerC"));
}

@Test
void runCommandLineRunnersAndApplicationRunnersUsingOrderOnBeanDefinitions() {
SpringApplication application = new SpringApplication(BeanDefinitionOrderRunnerConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
this.context = application.run("arg");
BeanDefinitionOrderRunnerConfig config = this.context.getBean(BeanDefinitionOrderRunnerConfig.class);
assertThat(config.runners).containsExactly("runnerA", "runnerB", "runnerC");
}

@Test
@SuppressWarnings("unchecked")
void runnersAreCalledAfterStartedIsLoggedAndBeforeApplicationReadyEventIsPublished(CapturedOutput output)
Expand Down Expand Up @@ -1616,6 +1627,31 @@ TestCommandLineRunner runnerA() {

}

@Configuration(proxyBeanMethods = false)
static class BeanDefinitionOrderRunnerConfig {

private final List<String> runners = new ArrayList<>();

@Bean
@Order
CommandLineRunner runnerC() {
return (args) -> this.runners.add("runnerC");
}

@Bean
@Order(Ordered.LOWEST_PRECEDENCE - 1)
ApplicationRunner runnerB() {
return (args) -> this.runners.add("runnerB");
}

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
CommandLineRunner runnerA() {
return (args) -> this.runners.add("runnerA");
}

}

@Configuration(proxyBeanMethods = false)
static class ExitCodeCommandLineRunConfig {

Expand Down

0 comments on commit 865203f

Please sign in to comment.