You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Shell 3.0如何禁用部分或全部内置命令?

在Spring Shell 3.0中禁用内置命令的实现方法

完全可以实现禁用指定内置命令的需求,以下是两种常用的实现方式:

方式一:通过自定义命令注册定制器灵活禁用

创建一个实现CommandRegistrationCustomizer的组件,针对目标命令的ID设置禁用状态:

import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.command.CommandRegistrationCustomizer;
import org.springframework.stereotype.Component;

@Component
public class DisableBuiltInCommandsCustomizer implements CommandRegistrationCustomizer {

    @Override
    public void customize(CommandRegistration registration) {
        String commandId = registration.getId();
        // 匹配需要禁用的内置命令ID
        if ("stacktrace".equals(commandId) || "script".equals(commandId)) {
            registration.enabled(false);
        }
    }
}

这种方式的优势是可以精准控制单个命令的启用状态,不会影响其他内置命令的正常使用。

方式二:排除对应命令的自动配置类

Spring Shell 3.0的内置命令是通过自动配置类注册的,直接在启动类中排除目标命令的自动配置类,即可彻底移除这些命令的注册:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.shell.command.ScriptCommandAutoConfiguration;
import org.springframework.shell.command.StacktraceCommandAutoConfiguration;

@SpringBootApplication(exclude = {
    ScriptCommandAutoConfiguration.class,
    StacktraceCommandAutoConfiguration.class
})
public class YourShellApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourShellApplication.class, args);
    }
}

这种方式会直接阻止目标命令的注册流程,适合需要彻底移除命令的场景。

内容的提问来源于stack exchange,提问作者D-Dᴙum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 01:39:58