Spring Shell——快速构建终端应用,自定义终端命令

Spring Shell是spring的一个子项目,通过Spring Shell可以快速构建一个终端应用。并且可以通过@ShellComponent注解很方便地自定义终端命令。

这篇文章就介绍一下如何在项目中使用Spring Shel

第一步:创建项目

创建一个springboot项目springboot-springshell

第二步:添加依赖

修改springboot依赖版本,添加spring-shell-starter的依赖。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.edu.sgu.www</groupId>
    <artifactId>springboot-springshell</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-springshell</name>
    <description>Spring Boot整合Spring Shell</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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.1.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

第三步:定义命令

使用@ShellComponent+@ShellMethod自定义命令。

1、无参数命令

java 复制代码
package cn.edu.sgu.www.shell.command;

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;

/**
 * @author heyunlin
 * @version 1.0
 */
@ShellComponent
public class Commands {

    @ShellMethod("初始化项目环境。")
    public String init() {
        // todo

        return "初始化项目环境完成...";
    }

}

2、有参数的命令

java 复制代码
package cn.edu.sgu.www.shell.command;

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;

/**
 * @author heyunlin
 * @version 1.0
 */
@ShellComponent
public class Commands {

    @ShellMethod("计算两个整数的和。")
    public int add(int a, int b) {
        return a + b;
    }

}

3、设置参数选项

比如设置参数默认值

java 复制代码
package cn.edu.sgu.www.shell.command;

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

/**
 * @author heyunlin
 * @version 1.0
 */
@ShellComponent
public class Commands {

    @ShellMethod("打招呼:")
    public String greet(@ShellOption(defaultValue = "沐雨橙风ιε") String name) {
        return "Hello " + name;
    }

}

4、设置参数校验

比如:设置参数最大值和最小值,可以使用Hibernate Validator的数据校验注解。

java 复制代码
package cn.edu.sgu.www.shell.command;

import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

/**
 * @author heyunlin
 * @version 1.0
 */
@ShellComponent
public class Commands {

    @ShellMethod("计算平方数。")
    public int sqrt(@Min(value = 1) @Max(value = 99) int n) {
        return n * n;
    }

}

第四步:使用命令

启动项目,然后在控制台上就可以直接使用各种命令了,方法的参数直接在命令后面输入。

项目已经上传到git,可按需获取~

Spring Boot整合Spring Shell案例项目https://gitee.com/muyu-chengfeng/springboot-springshell.git

相关推荐
小兔子3 小时前
os.path.join 遇到绝对路径会丢掉前面所有段,这条规则写在文档第二句
后端
小兔子4 小时前
Go 的接口是类型加值,所以赋了 nil 的 err 判断起来不等于 nil
后端
不一样的少年_4 小时前
不花一分额外 API 钱,用现成的 Claude Code / Codex / Cursor 搭建 AI 个人知识库
前端·后端·ai编程
用户6919026813394 小时前
Redis 1:简述Redis概念与Agent的联系
redis·后端·设计
艾思控驱动器4 小时前
手写一个电流环采样时序校核工具:为什么你的 FOC 电流总差几个百分点
后端
揣一尾游鱼yu4 小时前
内部类和Object笔记
java·开发语言·object·内部类
智驭未来掌门人4 小时前
桌面程序里的“1K 以内”:到底该限制哪一层像素(C++ Qt 版)
后端
reesun4 小时前
分层干不干净,要等你想换掉一层才见分晓:DuckDB 2.0 换掉 parser 靠的那道缝
后端
逸风尊者4 小时前
智能调度系统从0到1:系统架构设计方法
后端
你怎么知道我是队长4 小时前
C++零基础第3周:if/else、switch、for/while/do-while 控制流完全指南(附20段可运行代码+避坑清单)
java·开发语言·c++