1. What is Spring Shell?
Spring Shell is a member of the Spring ecosystem for developing command-line applications.https://projects.spring.io/spring-shell/ 。 Spring Shell is built onJLineOn top of that, integratedBean Validation APIImplement command parameter validation. Since version 2.0, Spring Shell can also be easily integrated with Spring Boot, directly using some of the very useful features provided by Spring Boot (e.g., packaging executable jar files).
2. Code engineering
Purpose: To test various Spring shell functions
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
The first hello word example
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 + "!");
}
}
some examples
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(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);
}
}
Controls the order in which commands are executed
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);
}
}
The above are just some of the key codes, all of which can be found in the repositories below
Code repositories
3. Testing
Package the Spring Boot application
mvn clean package -Dmaven.test.skip=true
Start the program
java -jar Spring-Shell-1.0-SNAPSHOT.jar
Execute the command, type help, and see all the commands
Execute the command
shell:>sum 1 2
1 + 2 = 3