当前位置: 首页 > article >正文

Spring Shell基于注解定义命令

当使用标准API时,bean上的方法将变为可执行命令,前提是:

  • bean类带有@ShellComponent注释,@ShellComponent是一个原型注释,本身用@Component进行标注,因此除了过滤机制外,你还可以使用它来声明bean,并且通过注释的value属性定义bean的名称。
  • 该方法带有@ShellMethod注释。
@ShellComponent
public class MyCommand {
    @ShellMethod
    public void mycommand() {
        System.out.println("---");
    }

    @ShellMethod(value = "Add numbers.", key = {"sum", "add"}, prefix = "--")
    public int add(int a, int b) {
        return a + b;
    }
}

@ShellMethod的value属性指定对命令的描述,可以通过help命令查看命令的使用方法:

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.

Demo Application
       hi: 

My Command
       mycommand: 
       sum, add: Add numbers.
二、分组group

默认情况下,命令根据其实现的类进行分组,将驼峰类名转换为单独的单词。可以通过如下方式指定分组(优先级按照说明顺序)

  • 在@ShellMethod注解上指定group属性;
  • 在定义命令的类上防止一个@ShellCommandGroup,这将改组应用于该类中定义的所有命令(除非如前所属被覆盖);
  • 在定义命令的包上防止一个@ShellCommandGroup(通过package-info.java),这适用于包中定义的所有命令(除非在方法或类级别重写,如前所述)

开源SDK:https://github.com/mingyang66/spring-parent


http://www.kler.cn/news/326524.html

相关文章:

  • 外包干了1个多月,技术明显退步了。。。。。
  • 3-基于容器安装carla
  • Python——判断文件夹/文件是否存在、删除文件夹/文件、新建文件夹
  • SpringAOP学习
  • 【C语言软开面经】
  • pdf提取文字:分享3款pdf文字提取软件,赶快收藏起来!
  • Unity开发绘画板——03.简单的实现绘制功能
  • 配置ssh后又报错git@github.com: Permission denied (publickey)
  • Linux【基础指令汇总】
  • 论文翻译 | LLaMA-Adapter :具有零初始化注意的语言模型的有效微调
  • SpringBoot+Thymeleaf发票系统
  • 【2025】springboot基于微信小程序记账本的设计与实现(源码+文档+调试+答疑)
  • kafka集群跨双网段及多网段通信问题解决(避免踩坑)
  • 享元(轻量级)模式
  • Spring Boot 进阶- Spring Boot入门程序详解
  • 初始docker以及docker的基本使用!!!
  • Cannon-es.js之Distance Constrait模拟布料
  • 【hot100-java】【合并两个有序链表】
  • MySQL数据库备份详解
  • Ubuntu下安装向日葵:闪退
  • SpirngBoot核心思想之一IOC
  • Leetcode 46 Permutation Leetcode 78 Subsets
  • AndroidStudio依赖报错
  • 力扣(leetcode)每日一题 1014 最佳观光组合
  • Android 开启相机一键拍照,一键录制
  • VirtualService和destinationRule
  • 大数据毕业设计选题推荐-国潮男装微博评论数据分析系统-Hive-Hadoop-Spark
  • Ranger集成CDH6.3.1详细步骤
  • 『功能项目』下载Mongodb【81】
  • C++中string的使用