The company has an urgent need to call the server interface through websocket. Due to the limited time, it decided to make a command-line script demo for the customer first. After learning the spring shell, I wrote a blog to record it.
1.spring shell environment
spring boot needs to be introduced
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> </parent>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.shell</groupId> <artifactId>spring-shell-starter</artifactId> <version>2.0.0.RELEASE</version> </dependency>
I also configured maven's packaging tool
<build> <plugins> <plugin> <!-- Configure packaging tools --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.5.RELEASE</version> <configuration> <!-- Main entrance of the project --> <mainClass>com.bjnet.test.Main</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2. main method of spring shell
The springboot project is the same as springboot
@SpringBootApplication public class Main { public static void main(String[] args) { SpringApplication app = new SpringApplication(Main.class); app.setBannerMode(Banner.Mode.OFF); app.setWebApplicationType(WebApplicationType.NONE); app.run(args); } }
3. Spring shell definition command
- @ShellComponet: declares that this class is registered as a script component of spring
- @shellMehod: declare a command. The default method name is the command name. If there are multiple words, the generated commands are separated by -, for example - method name: openUser @ shellMehod. The default command name generated is open user
@ShellComponent public class CalculatorCommands { @ShellMethod("Calculates the addition of two integers") public int add(int a, int b){ return a + b; } }
Packaged Java - jar XXX Jar run add a b
4. Spring shell custom command name
Customize the add command to - a
@ShellComponent public class CalculatorCommands { @ShellMethod(value = "Calculates the addition of two integers",key = {"-a"}) public int add(int a, int b){ return a + b; } }
The command changes to - A, B, and the add command fails
5. Default value of spring shell parameters
When the input command requires parameters, you can specify a default value in the parameter list. When the user does not enter parameters, the default value will be used
@ShellComponent public class CalculatorCommands { @ShellMethod(value = "Calculates the addition of two integers",key = {"-a"}) public int add(@ShellOption(defaultValue = "0")int a){ return a + b; } }
The default value of a of the - a command is 0
Note that defaultvalue = "0" double quotation marks are indispensable here
Double quotes are required for int spring
6. Spring shell command prompt customization
@Bean public PromptProvider promptProvider() { return () -> new AttributedString("aaa=>", AttributedStyle.DEFAULT.foreground(AttributedStyle.BLUE)); }
The shell before the command changes to blue AAA = >
In AttributedStyle, there are many to define the bold {hidden color of text, which can be customized as needed
7. Spring shell command restrictions
@ShellMethod(value = "close WebSocket connect",key = {"-c"}) public void close(){ app.stop(); } public Availability closeAvailability(){ return isConnect ? Availability.available(): Availability.unavailable("you are not connect"); }
When the user enters the - c command, he will first execute the logic in return to determine whether it is connected. He can only enter the - c close command after connecting. Otherwise, an error will be reported, and the error prompt will be written in unavailable.
The closeavailability here corresponds to the method name above. close must correspond to {otherwise it will not take effect
Of course, when you need to use one authentication multiple times, you can also specify the name of availability
@ShellMethod(value = "",key = {"-s"}) @ShellMethodAvailability({"isLogin"}) public void start(){ System.out.println("start"); } } @ShellMethod(value = "",key = {"-c"}) @ShellMethodAvailability({"isLogin"}) public void close(){ System.out.println("close"); } } public Availability isLogin(){ return isLogin ? Availability .available():Availability .unavailable("you are not login"); }
Start and close commands can only be run after logging in
@ShellMethodAvailability specifies the method name to realize reuse
quote: https://blog.csdn.net/zongf0504?spm=1001.2014.3001.5509