入门

为了了解 Spring Shell 的功能,我们可以编写一个简单的 hello world shell 应用程序,它带有一个简单的参数。

Spring Shell 基于 Spring Boot 3.4.0 和 Spring Framework 6.2.0,因此需要 JDK 17

创建项目

为了本教程的目的,我们使用 start.spring.io 创建一个简单的 Spring Boot 应用程序,您可以在其中选择 Spring Shell 依赖项。这个最小的应用程序只依赖于 spring-boot-starterspring-shell-starter

start.spring.io 上的 *Spring Shell* 版本通常是最新的发行版本。

使用 maven,您应该会有类似以下的内容

<properties>
    <spring-shell.version>3.4.0</spring-shell.version>
</properties>

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.shell</groupId>
            <artifactId>spring-shell-dependencies</artifactId>
            <version>${spring-shell.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

使用 gradle,您应该会有类似以下的内容

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.shell:spring-shell-starter'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.shell:spring-shell-dependencies:3.4.0"
    }
}
版本最高到 3.2.x 默认启用了所有 runner,从 3.3.x 开始默认只启用了 NonInteractiveShellRunner。这意味着您需要启用 InteractiveShellRunner 才能获得 REPL。
spring:
  shell:
    interactive:
      enabled: true
鉴于 Spring Shell 会启动 REPL (Read-Eval-Print-Loop),因为存在此依赖项,您需要在构建时跳过测试 (-DskipTests),或者删除由 start.spring.io 生成的示例集成测试。如果不删除,集成测试将创建 Spring ApplicationContext,并且根据您的构建工具,可能会卡在 eval 循环中或因 NPE 而崩溃。

编译后,可以在交互模式下运行

$ $JAVA_HOME/bin/java -jar demo-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v3.4.0)

2022-09-13T18:42:12.818+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Starting DemoApplication using Java 17.0.4 on ...
2022-09-13T18:42:12.821+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: No active profile set, falling back to 1 default profile: "default"
2022-09-13T18:42:13.606+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Started DemoApplication in 1.145 seconds (process running for 1.578)
shell:>help
AVAILABLE COMMANDS

Built-In Commands
       help: Display help about available commands
       stacktrace: Display the full stacktrace of the last error.
       clear: Clear the shell screen.
       quit, exit: Exit the shell.
       history: Display or save the history of previously run commands
       version: Show version info
       script: Read and execute commands from a file.

或在非交互模式下运行

$JAVA_HOME/bin/java -jar demo-0.0.1-SNAPSHOT.jar help

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v3.4.0)

2022-09-13T18:42:12.818+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Starting DemoApplication using Java 17.0.4 on ...
2022-09-13T18:42:12.821+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: No active profile set, falling back to 1 default profile: "default"
2022-09-13T18:42:13.606+01:00  INFO 12644 --- [           main] com.example.demo.DemoApplication
: Started DemoApplication in 1.145 seconds (process running for 1.578)
AVAILABLE COMMANDS

Built-In Commands
       help: Display help about available commands
       stacktrace: Display the full stacktrace of the last error.
       clear: Clear the shell screen.
       quit, exit: Exit the shell.
       history: Display or save the history of previously run commands
       version: Show version info
       script: Read and execute commands from a file.
查看 日志记录,了解如何让日志记录更好地与 shell 应用程序配合使用。

您的第一个命令

现在我们可以添加第一个命令。为此,创建一个新类(名称随意),并用 @ShellComponent 注解它,它是 @Component 的变体,用于限制扫描候选命令的类集。

然后我们可以创建一个 helloWorld 方法,它接受 String 作为参数并返回加上“Hello world”的内容。添加 @ShellMethod 并可以选择使用 key 参数更改命令名称。您可以使用 @ShellOption 定义参数的默认值,如果在运行命令时未提供该参数。

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

@ShellComponent
public class MyCommands {

	@ShellMethod(key = "hello-world")
	public String helloWorld(
		@ShellOption(defaultValue = "spring") String arg
	) {
		return "Hello world " + arg;
	}
}

新的 hello-world 命令在 help 中可见

My Commands
       hello-world:

并且您可以运行它

shell:>hello-world
Hello world spring

shell:>hello-world --arg boot
Hello world boot

本文档的其余部分将深入探讨整个 Spring Shell 编程模型。