1. 程式人生 > >採用 spring shell開發 java命令列工具

採用 spring shell開發 java命令列工具

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