Skip to content

Commit 0ba60c4

Browse files
committed
Fix running shell in the Spring Boot applications
gh-1203 Signed-off-by: Piotr Olaszewski <[email protected]>
1 parent 328f23a commit 0ba60c4

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/ApplicationRunnerAutoConfiguration.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,36 @@
1515
*/
1616
package org.springframework.shell.boot;
1717

18+
import org.springframework.beans.factory.ObjectProvider;
19+
import org.springframework.boot.ApplicationArguments;
20+
import org.springframework.boot.ApplicationRunner;
1821
import org.springframework.boot.autoconfigure.AutoConfiguration;
1922
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2023
import org.springframework.boot.context.event.ApplicationReadyEvent;
2124
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2225
import org.springframework.context.ApplicationListener;
2326
import org.springframework.context.annotation.Bean;
27+
import org.springframework.shell.core.ShellRunner;
28+
import org.springframework.util.ClassUtils;
2429

30+
/**
31+
* @author Janne Valkealahti
32+
* @author Chris Bono
33+
* @author Mahmoud Ben Hassine
34+
* @author Piotr Olaszewski
35+
*/
2536
@AutoConfiguration
2637
@EnableConfigurationProperties(SpringShellProperties.class)
2738
public class ApplicationRunnerAutoConfiguration {
2839

40+
@Bean
41+
public ApplicationRunner applicationRunner(ObjectProvider<ShellRunner> shellRunner) {
42+
return new ShellApplicationRunner(shellRunner);
43+
}
44+
2945
@Bean
3046
@ConditionalOnProperty(prefix = "spring.shell.context", name = "close", havingValue = "true")
31-
public ApplicationReadyEventListener applicationReadyEventListener() {
47+
public ApplicationListener<ApplicationReadyEvent> applicationReadyEventListener() {
3248
return new ApplicationReadyEventListener();
3349
}
3450

@@ -43,4 +59,22 @@ public void onApplicationEvent(ApplicationReadyEvent event) {
4359

4460
}
4561

62+
record ShellApplicationRunner(ObjectProvider<ShellRunner> shellRunner) implements ApplicationRunner {
63+
@Override
64+
public void run(ApplicationArguments args) throws Exception {
65+
shellRunner.orderedStream().forEachOrdered(runner -> {
66+
try {
67+
boolean run = runner.run(args.getSourceArgs());
68+
if (run) {
69+
return;
70+
}
71+
}
72+
catch (Exception e) {
73+
throw new IllegalStateException(
74+
"Unable to run '" + ClassUtils.getShortName(runner.getClass()) + "'", e);
75+
}
76+
});
77+
}
78+
}
79+
4680
}

0 commit comments

Comments
 (0)