02.SpringShell自定义命令-@ShellMethod

版权声明:转载请注明出处! https://blog.csdn.net/zongf0504/article/details/86663656

@ShellMethod 用于声明一个方法为Shell 命令, 默认命令名称为方法名, 命令所属分组为方法所在类类名, 可以通过自定义属性来做调整. 默认情况下, SpringShell 会将方法名作为命令名, 当方法名为驼峰方式时, SpringShell会自动替换为用-分隔的命令. 比如说 connectOracle 方法会被定义为connect-oracle 命令.

1. @ShellMethod 常用属性

  • value: 命令注释, 通过help 查看命令时显示内容
  • key: 指定命令别名, 可以同时定义多个别名. 当使用key定义别名之后, 方法名则不再是别名.
  • prefix: 指定名称参数前缀, 默认为–
  • group: 指定命令所属组, 默认分组为类名

2. 属性测试

2.1 自定义属性

@ShellMethod(value = "计算两个整数的加法", key = {"add","sum"}, group = "group1", prefix = "-")
public int add(int a, int b){
    return a + b;
}

2.2 查看分组名称

只能通过查看所有命令来看分组名称, 未指定分组名称前, group1为组件所在类名

shell:>help
AVAILABLE COMMANDS

Built-In Commands
        clear: Clear the shell screen.
        exit, quit: Exit the shell.
        help: Display help about available commands.
        script: Read and execute commands from a file.
        stacktrace: Display the full stacktrace of the last error.

group1
        add, sum: 计算两个整数的加法

2.3 调用命令

shell:>add 2 1
3
shell:>sum 2 1
3

2.4 参数前缀

shell:>add -a 2 -b 1
3

猜你喜欢

转载自blog.csdn.net/zongf0504/article/details/86663656