为了了解 Spring Shell 提供了什么,我们可以编写一个具有简单参数的 hello world shell 应用程序。Spring中文文档

Spring Shell 基于 Spring Boot 3.3.0 和 Spring Framework 6.1.8,因此需要 JDK 17
Spring Shell 基于 Spring Boot 3.3.0 和 Spring Framework 6.1.8,因此需要 JDK 17

创建项目

在本教程中,我们通过以下方式创建一个简单的 Spring Boot 应用程序 使用 start.spring.io,您可以在其中选择 Spring Shell 依赖项。 此最小应用程序仅依赖于 和 。spring-boot-starterspring-shell-starterSpring中文文档

Spring Shell版本通常是最新版本。start.spring.io

使用 maven,您应该拥有以下内容:Spring中文文档

<properties>
    <spring-shell.version>3.3.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,您应该拥有以下内容:Spring中文文档

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.3.0"
    }
}
鉴于 Spring Shell 启动 REPL(Read-Eval-Print-Loop),因为这 存在依赖关系,您需要在构建 () 时跳过测试 在本教程中,或删除生成的示例集成测试 平价 start.spring.io.如果不删除它,则集成测试将创建 Spring,并且根据您的构建工具,会卡在 eval 循环或因 NPE 而崩溃。-DskipTestsApplicationContext

编译后,它可以在交互模式下运行:Spring中文文档

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

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v3.3.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.

或者在非交互模式下:Spring中文文档

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

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v3.3.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 应用程序会更好。
Spring Shell版本通常是最新版本。start.spring.io
鉴于 Spring Shell 启动 REPL(Read-Eval-Print-Loop),因为这 存在依赖关系,您需要在构建 () 时跳过测试 在本教程中,或删除生成的示例集成测试 平价 start.spring.io.如果不删除它,则集成测试将创建 Spring,并且根据您的构建工具,会卡在 eval 循环或因 NPE 而崩溃。-DskipTestsApplicationContext
查看日志记录使日志记录正常工作 使用 shell 应用程序会更好。

您的第一个命令

现在我们可以添加第一个命令。为此,请创建一个新类(随心所欲地命名),然后 注释它,它是用于限制的变体 扫描候选命令的类集。@ShellComponent@ComponentSpring中文文档

然后我们可以创建一个方法,该方法作为参数和 返回“Hello world”。添加和更改命令名称(可选) using 参数。可用于定义参数默认值 如果在运行命令时未给出。helloWorldString@ShellMethodkey@ShellOptionSpring中文文档

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 命令变得可见,以帮助Spring中文文档

My Commands
       hello-world:

你可以运行它:Spring中文文档

shell:>hello-world
Hello world spring

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

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