采用 spring shell开发 java命令行工具

由于spring shell库的使用其官网以及IBM上都有很详细的介绍和使用示例,本篇主要是对资源的罗列汇总和给出一个完整的简单的示例。
1、spring shell 官网地址: http://projects.spring.io/spring-shell/
spring shell
2、spring shell官网使用文档地址(在此罗列的是版本2.0.0):https://docs.spring.io/spring-shell/docs/2.0.0.RELEASE/reference/htmlsingle/
spring shell reference

3、spring shell 中文使用介绍(IBM文档—《使用 Spring Shell 开发 Java 命令行应用》)地址:https://www.ibm.com/developerworks/cn/java/spring-shell-application/index.html?ca=drs-&utm_source=tuicool&utm_medium=referral

4、spring shell github地址:https://github.com/spring-projects/spring-shell

5、springboot+springshell简单示例代码
1)Maven配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.shell</groupId>
    <artifactId>spring-shell-starter</artifactId>
</dependency>

2)springboot启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

3)spring shell命令

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
@ShellComponent
public class Commands{
    @ShellMethod("sum函数")
    public int add(int param1,int param2,@ShellOption(defaultValue=0)String param3) {
        return param1+param2+param3;
    }
}

4)运行
采用maven命令:mvn spring-boot:run 运行即可,运行结果如下图:
spring shell run

猜你喜欢

转载自blog.csdn.net/fengshuiyue/article/details/80267428