1515 */
1616package org .springframework .shell .boot ;
1717
18+ import org .springframework .beans .factory .ObjectProvider ;
19+ import org .springframework .boot .ApplicationArguments ;
20+ import org .springframework .boot .ApplicationRunner ;
1821import org .springframework .boot .autoconfigure .AutoConfiguration ;
1922import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
2023import org .springframework .boot .context .event .ApplicationReadyEvent ;
2124import org .springframework .boot .context .properties .EnableConfigurationProperties ;
2225import org .springframework .context .ApplicationListener ;
2326import 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 )
2738public 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