开始

为了了解 Spring Shell 的功能,我们可以写一个简单的 hello world shell 应用,它有一个简单的参数。spring-doc.cadn.net.cn

Spring Shell 基于 Spring Boot 3.5.4 和 Spring Framework 6.2.9,因此需要 JDK 17

创建项目

为了本教程,我们创建一个简单的 Spring Boot 应用程序: 使用 start.spring.io,你可以选择 Spring Shell 依赖。 这一最小应用仅依赖于Spring靴Starters弹壳起动器.spring-doc.cadn.net.cn

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

Maven时,你应该有类似这样的条件:spring-doc.cadn.net.cn

<properties>
    <spring-shell.version>3.4.1</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-doc.cadn.net.cn

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.1"
    }
}
版本至以下3.2.x默认启用所有运行器,从以下开始3.3.x非交互式壳跑者默认启用。这意味着你需要启用互动壳跑者为了获得REPL。
spring:
  shell:
    interactive:
      enabled: true
鉴于Spring Shell启动REPL(读取-评估-打印-循环),因为 存在依赖性,你需要在构建时跳过测试 (-Dskip测试) 在整个教程中,或者删除生成的样本积分测试 start.spring.io。如果不移除它,积分测试会生成 Spring应用上下文而且,根据你的构建工具,它会一直卡在游戏里 评估循环或与NPE崩溃。

编译完成后,它可以以交互模式运行:spring-doc.cadn.net.cn

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

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

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-doc.cadn.net.cn

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

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

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.
看看《让Logging成为工作工具》吧 用shell应用更好。

你的第一次指挥

现在我们可以添加第一个指令了。为此,创建一个新类(随你意愿命名),然后 注释为@ShellComponent这是 的变体@Component用于限制 扫描候选命令的类集合。spring-doc.cadn.net.cn

然后我们可以创建helloWorld该方法字符串作为一个论元,并且 回以“你好,世界”。加@ShellMethod并可选择更改命令名称 用钥匙参数。你可以使用@ShellOption定义参数默认值 如果执行命令时没有给出。spring-doc.cadn.net.cn

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-doc.cadn.net.cn

My Commands
       hello-world:

你可以运行它:spring-doc.cadn.net.cn

shell:>hello-world
Hello world spring

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

本文其余部分将更深入地探讨整个 Spring Shell 编程模型。spring-doc.cadn.net.cn