java - 在没有 Spring Boot 的情况下运行 Spring Shell 2

标签 java spring spring-boot spring-shell

我已经成功使用 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(...);

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

提前致谢!

最佳答案

通过提取 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);
    }
}

关于java - 在没有 Spring Boot 的情况下运行 Spring Shell 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49480693/

相关文章:

Java 意外类型错误

java - Spring MVC 中的 PUT 请求

spring-boot - 如何在 WildFly 10 中将 exploded war 部署为文件夹

java - 运行测试时获取 "java.lang.IllegalArgumentException: Decode argument cannot be null"

java - 使用 java.bean API 检查非公共(public)类的实例

java - Java中运算符重载的困惑

java - java -jar 选项是否改变类路径选项

spring - STOMP 或 XMPP - 通过 websocket

spring - 如何给定一个参数默认值?

java - 在 Spring Boot 中将 Apache Mahout 与 ElasticSearch 集成