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
相关产品推荐
相关产品推荐

