Spring Shell 基本使用

Spring Shell Spring Boot About 848 words

添加命令

@ShellComponent:等同于@Component,使Spring扫描进IoC容器。

@ShellMethod:定义命令。 - value:命令的描述信息。 - key:命令的名称,默认是方法名,可使用key选项修改。

@ShellComponent
public class MyCommands {

    @ShellMethod(value = "Add two integers together.", key = "sum")
    // @ShellMethod("Add two integers together.")
    public int add(int a, int b) {
        return a + b;
    }
}

查看命令列表

运行程序后,输入help,即可查看可使用的命令。

shell:>help
AVAILABLE COMMANDS

Built-In Commands
       help: Display help about available commands
       clear: Clear the shell screen.
       quit, exit: Exit the shell.
       history: Display or save the history of previously run commands
       version: Show version info

My Commands
       sum: Add two integers together.

使用命令

可以按形参上的参数传入参数(不用指定参数名),需按参数定义的顺序传入。

可以使用--arg双连字符加参数名,指定传入的参数,可以不用按形参的定义顺序。

shell:>sum 1 2
3
shell:>sum --a 1 --b 2
3
Views: 771 · Posted: 2023-03-07

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh