2

我已经设法使用 Spring Boot 启动 Spring Shell:

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
    }
}

我的所有@ShellComponent课程都被检测到,我可以按预期使用 shell。

现在我想在没有 Spring Boot 的情况下运行 shell,我希望它看起来像这样

Shell shell = context.getBean(Shell.class);
shell.run(...);

我应该采取什么方法来自己配置所有必需的依赖项?

提前致谢!

4

3 回答 3

3

通过提取 ebottard 链接的必要部分(谢谢!)我终于设法像我想要的那样运行 shell:

@Configuration
@Import({
        SpringShellAutoConfiguration.class,
        JLineShellAutoConfiguration.class,

        JCommanderParameterResolverAutoConfiguration.class,
        StandardAPIAutoConfiguration.class,

        StandardCommandsAutoConfiguration.class,
})
public class SpringShell {

    public static void main(String[] args) throws IOException {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringShell.class);
        Shell shell = context.getBean(Shell.class);
        shell.run(context.getBean(InputProvider.class));
    }

    @Bean
    @Autowired
    public InputProvider inputProvider(LineReader lineReader, PromptProvider promptProvider) {
        return new InteractiveShellApplicationRunner.JLineInputProvider(lineReader, promptProvider);
    }
}
于 2018-03-26T16:38:07.813 回答
1

请参阅此示例,该示例显示了如何在不依赖自动配置的情况下连接所有内容。

于 2018-03-26T07:37:27.790 回答
0

没有 Spring Boot 我这样写:

@Configuration
@ComponentScan(value = {"path.to.commands", "org.springframework.shell.commands", "org.springframework.shell.converters", "org.springframework.shell.plugin.support"})
public class TestShell {
private static String[] args;

@Bean("commandLine")
public CommandLine getCommandLine() throws IOException {
    return SimpleShellCommandLineOptions.parseCommandLine(args);
}

@Bean("shell")
public JLineShellComponent getShell() {
    return new JLineShellComponent();
}

public static void main(String[] args) {
    TestShell.args = args;
    System.setProperty("jline.terminal", "none");

    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(TestShell.class);
    ctx.registerShutdownHook();

    JLineShellComponent shell = ctx.getBean(JLineShellComponent.class);

    shell.start();
    shell.waitForComplete();
    ExitShellRequest exitShellRequest = shell.getExitShellRequest();
    if (exitShellRequest == null)
        exitShellRequest = ExitShellRequest.NORMAL_EXIT;
    System.exit(exitShellRequest.getExitCode());
}

}

和命令类:

@Component
public class Hello implements CommandMarker {
@CliCommand(value="hi", help = "say hello.")
public String hi() {
    return "hello";
}

}

参见 org.springframework.shell.Bootstrap。

于 2018-11-14T13:47:22.367 回答