切換語言為:簡體
Spring Boot 整合 Spring Shell 快速入門 Demo

Spring Boot 整合 Spring Shell 快速入門 Demo

  • 爱糖宝
  • 2024-05-22
  • 2086
  • 0
  • 0

1.什麼是Spring Shell?

Spring Shell是Spring生態中的一員,用於開發命令列應用程式,官網:https://projects.spring.io/spring-shell/ 。 Spring Shell構建在JLine之上,整合Bean Validation API實現命令引數校驗。 從2.0版本開始,Spring Shell還可以非常方便地與Spring Boot進行整合,直接使用Spring Boot提供的一些非常實用的功能(如:打包可執行jar檔案)。

Spring Boot 整合 Spring Shell 快速入門 Demo

2.程式碼工程

實驗目的:測試各種Spring shell功能

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>Spring-Shell</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.shell</groupId>
            <artifactId>spring-shell-starter</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

command

第一個hello word例子

package com.et.spring.shell.command;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
import javax.validation.constraints.Size;
import java.util.List;
@ShellComponent
@ShellCommandGroup("HelloWorld")
public class HelloWorld {
    @ShellMethod("Say hello")
    public void hello(@ShellOption(defaultValue = "World")String name) {
        System.out.println("hello, " + name + "!");
    }
}

各種常見引數設定

package com.et.spring.shell.command;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
import javax.validation.constraints.Size;
import java.util.List;
@ShellComponent
@ShellCommandGroup("Calculator")
public class Calculator {
    // give multi name
    //@ShellMethod for group
    //@ShellMethod(value = "Add numbers.", key = {"sum", "addition"})
    @ShellMethod(value = "Add numbers.",  key = {"sum", "addition"}, prefix = "-", group = "Cal")
    public void add(int a, int b) {
        int sum = a + b;
        System.out.println(String.format("%d + %d = %d", a, b, sum));
    }
    @ShellMethod("Echo command help")
    public void myhelp(@ShellOption({"-C", "--command"}) String cmd) {
        System.out.println(cmd);
    }
    // params is collection
    @ShellMethod("Add by list")
    public void addByList(@ShellOption(arity = 3) List<Integer> numbers) {
        int s = 0;
        for(int number : numbers) {
            s += number;
        }
        System.out.println(String.format("s=%d", s));
    }
    @ShellMethod("Echo.")
    public void echo(String what) {
        System.out.println(what);
    }
    // use @Size validate param length
    @ShellMethod("Change password")
    public void changePwd(@Size(min = 6, max = 30) String pwd) {
        System.out.println(pwd);
    }
}

控制命令的執行順序

package com.et.spring.shell.command;
import org.springframework.shell.Availability;
import org.springframework.shell.standard.ShellCommandGroup;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
@ShellComponent
@ShellCommandGroup("Downloader")
public class Downloader {
    private boolean connected = false;
    @ShellMethod("Connect server")
    public void connect() {
        connected = true;
    }
    @ShellMethod("Download file")
    public void download() {
        System.out.println("Downloaded.");
    }
    // download availability
    public Availability downloadAvailability() {
        return connected ? Availability.available():Availability.unavailable("you are not connected");
    }
}

DemoApplication.java

package com.et.spring.shell;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

以上只是一些關鍵程式碼,所有程式碼請參見下面程式碼倉庫

程式碼倉庫

https://github.com/Harries/springboot-demo

3.測試

打包Spring Boot應用

mvn clean package -Dmaven.test.skip=true

啟動程式

java -jar Spring-Shell-1.0-SNAPSHOT.jar

執行命令, 輸入help,檢視所有命令

Spring Boot 整合 Spring Shell 快速入門 Demo

執行命令

shell:>sum 1 2
1 + 2 = 3

4.引用

https://docs.spring.io/spring-shell/reference/getting-started.html

https://www.cnblogs.com/nuccch/p/11067342.html

http://www.liuhaihua.cn/archives/710583.html

0則評論

您的電子郵件等資訊不會被公開,以下所有項目均必填

OK! You can skip this field.