|
该版本仍在开发中,尚未被视为稳定。最新稳定版本请使用Spring Shell 3.4.1! |
开始
为了了解 Spring Shell 的功能,我们可以写一个简单的 hello world shell 应用,它有一个简单的参数。
创建项目
为了本教程,我们创建一个简单的 Spring Boot 应用程序:
使用 start.spring.io,你可以选择 Spring Shell 依赖。
这一最小应用仅依赖于Spring靴Starters和弹壳起动器.
Spring Shell版本start.spring.io通常是最新版本。 |
用Maven时,你应该有类似这样的条件:
<properties>
<spring-shell.version>4.0.0-SNAPSHOT</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:4.0.0-SNAPSHOT"
}
}
版本至以下3.2.x默认启用所有运行器,从以下开始3.3.x只非交互式壳跑者默认启用。这意味着你需要启用互动壳跑者为了获得REPL。 |
spring:
shell:
interactive:
enabled: true
鉴于Spring Shell启动REPL(读取-评估-打印-循环),因为
存在依赖性,你需要在构建时跳过测试 (-Dskip测试)
在整个教程中,或者删除生成的样本积分测试
start.spring.io。如果不移除它,积分测试会生成
Spring应用上下文而且,根据你的构建工具,它会一直卡在游戏里
评估循环或与NPE崩溃。 |
编译完成后,它可以以交互模式运行:
$ $JAVA_HOME/bin/java -jar demo-0.0.1-SNAPSHOT.jar
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
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应用更好。 |
你的第一次指挥
现在我们可以添加第一个指令了。为此,创建一个新类(随你意愿命名),然后
注释为@Command这是 的变体@Component用于限制
扫描候选命令的类集合。
然后我们可以创建helloWorld该方法字符串作为一个论元,并且
回以“你好,世界”。加@Command并可选择更改命令名称
用命令参数。你可以使用@Option定义参数默认值
如果执行命令时没有给出。
package com.example.demo;
import org.springframework.shell.command.annotation.Command;
import org.springframework.shell.command.annotation.Option;
@Command
public class MyCommands {
@Command(command = "hello-world")
public String helloWorld(
@Option(defaultValue = "spring") String arg
) {
return "Hello world " + arg;
}
}
新的hello-world命令变得可见,以帮助:
My Commands
hello-world:
你可以运行它:
shell:>hello-world
Hello world spring
shell:>hello-world --arg boot
Hello world boot
本文其余部分将更深入地探讨整个 Spring Shell 编程模型。