官网:https://projects.spring.io/spring-shell/。html
Spring Shell除了提供一些经常使用的内置命令以外,还容许开发者对一些默认功能进行定制。java
禁用Spring Shell的内置命令很是简单,只须要在pom.xml文件中进行简单配置便可,以下所示:spring
<!-- Spring Shell --> <dependency> <groupId>org.springframework.shell</groupId> <artifactId>spring-shell-starter</artifactId> <version>2.0.0.RELEASE</version> <exclusions> <!-- 禁用内置命令 --> <exclusion> <groupId>org.springframework.shell</groupId> <artifactId>spring-shell-standard-commands</artifactId> </exclusion> </exclusions> </dependency>
shell:>help No command found for 'help' shell:>exit No command found for 'exit' shell:>
彻底禁用了全部内置命令以后,将没法经过help
命令查询其余命令信息,也不能再使用exit
命令退出应用。
所以,若是有须要的状况下,应该只是禁用某些内置命令。shell
若是须要禁用指定内置命令,须要在代码中设置对应的命令属性为false,格式为:spring.shell.command.<command>.enabled=true
。
例如,须要禁用help
命令:app
@SpringBootApplication public class TestSpringshellApplication { public static void main(String[] args) { String[] disabledCommands = new String[]{"--spring.shell.command.help.enabled=false"}; String[] fullArgs = StringUtils.concatenateStringArrays(args, disabledCommands); SpringApplication.run(TestSpringshellApplication.class, fullArgs); } }
# help命令将再也不能使用 shell:>help No command found for 'help' Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace. shell:>exit
若是禁用的是其余命令,如:clear
,在Spring Shell应用启动以后经过help
命令再也不能看被禁用的命令了。ide
@SpringBootApplication public class TestSpringshellApplication { public static void main(String[] args) { // 禁用了内置的clear命令 String[] disabledCommands = new String[]{"--spring.shell.command.clear.enabled=false"}; String[] fullArgs = StringUtils.concatenateStringArrays(args, disabledCommands); SpringApplication.run(TestSpringshellApplication.class, fullArgs); } }
shell:>help AVAILABLE COMMANDS Built-In Commands 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.
显然,在禁用了指定的内置命令以后,经过help
命令将不能看到该命令了。ui
若是但愿重写内置命令的实现,能够经过实现接口org.springframework.shell.standard.commands.<Command>.Command
来完成(如:须要重写clear命令的实现,实现接口org.springframework.shell.standard.commands.Clear.Command
)。
以下为重写内置命令script的实现:this
import org.springframework.shell.standard.ShellComponent; import org.springframework.shell.standard.ShellMethod; import org.springframework.shell.standard.commands.Script; // 实现接口org.springframework.shell.standard.commands.Script.Command @ShellComponent public class MyScript implements Script.Command { // 注意:命令名称与内置命令保持一致 @ShellMethod("Read and execute commands from a file.") public void script() { / // 实现自定义逻辑 System.out.println("override default script command"); } }
有意思的是,此时在内置命令“Built-In Commands”分组中将不能看到script
命令了,而是在自定义的分组中,spa
shell:>help AVAILABLE COMMANDS Built-In Commands # 在内置命令分组中看不到重写的命令了 clear: Clear the shell screen. exit, quit: Exit the shell. help: Display help about available commands. stacktrace: Display the full stacktrace of the last error. My Script # 重写的命令此时在自定义分组中 scriptdo: Read and execute commands from a file.
若是但愿被覆盖的内置命令依然可以在“Built-In Commands”分组中看到,能够经过注解@ShellMethod
的group属性指定。.net
// 指定被覆盖的内置命令分组为“Built-In Commands” @ShellMethod(value = "Read and execute commands from a file.", group = "Built-In Commands") public void script() { System.out.println("override default script command"); }
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. shell:>script override default script command
默认状况下,Spring Shell启动以后显示的是一个黄色的命令提示符(shell:>
)等待用户输入。
能够经过Spring Shell提供的接口org.springframework.shell.jline.PromptProvider
对该命令提示符进行定制。
// 经过实现接口org.springframework.shell.jline.PromptProvider定制命令提示符 import org.jline.utils.AttributedString; import org.jline.utils.AttributedStyle; import org.springframework.shell.jline.PromptProvider; import org.springframework.stereotype.Component; @Component public class MyPromptProvider implements PromptProvider { @Override public AttributedString getPrompt() { // 定制命令提示符为红色的“#” return new AttributedString("#", AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)); } }
以下为定制的命令提示符:
Spring Shell提供了2个默认的ApplicationRunner
,用于实现命令行选项的行为。
1.InteractiveShellApplicationRunner
用于启动交互式界面,接收用户输入命令。
2.ScriptShellApplicationRunner
用于在应用启动时从程序参数中读取指定文件中的命令并执行,具体来说:将多个命令写在文件中,并经过参数的形式将包含了批量命令的文件路径传递给程序,传递的文件路径参数必须以“@”开始,以下示例:
$ java -jar /home/test/sun/workspace/test-springshell/target/test-springshell-0.0.1-SNAPSHOT.jar @/home/test/cmd
文件/home/test/cmd
中的内容为:
$ cat /home/test/cmd help
这样,在启动程序时,将会自动执行/home/test/cmd
文件中的命令(若是文件不存在,启动应用时报错)。
值得注意的是: 当在程序参数中存在“@local_file_path”这样的参数时,应用启动后执行完文件“local_file_path”内命令以后就退出了,不会进入交互式命令行界面(上述示例中,应用启动后执行help
命令以后就退出了)。
若是Spring Shell默认提供的上述2个ApplicationRunner
没法知足需求,能够自定义其余的命令行选项行为,直接实现接口org.springframework.boot.ApplicationRunner
便可。
默认状况下,Spring Shell使用标准的Spring类型转换机制将命令行的文本参数转换为指定的类型。
实际上,Spring Shell是经过DefaultConversionService
注册Converter<S, T>
,GenericConverter
或者ConverterFactory<S, R>
类型的Bean对象来实现对命令行参数进行类型转换的。
换句话说,若是咱们须要自定义类型转换器,只须要简单实现接口org.springframework.core.convert.converter.Converter<S, T>
就能够了。
// 自定义类型 public class Food { private String value = null; public Food(String value) { this.value = value; } @Override public String toString() { return new StringBuilder() .append("Food{").append("value='").append(value).append("'}") .toString(); } } // 自定义类型转换器 @Component public class MyConverter implements Converter<String, Food> { @Override public Food convert(String s) { // 将输入参数转换为Food类型实例 return new Food(s); } } // 使用自定义转换类型 @ShellComponent public class ConvertionCmd { // 在命令方法中直接能够获取Food对象,这是经过前面的自定义类型转换器MyConverter实现的 @ShellMethod("Conversion food") public String food(Food food) { return food.toString(); } }
在命令行指定命令food
:
#food apple Food{value='apple'}
显然,经过自定义类型转换器能够实现对命令参数的特殊处理,很是实用。
【参考】
http://www.javashuo.com/article/p-hsvrdiyq-ce.html SpringBoot之CommandLineRunner接口和ApplicationRunner接口
https://www.jianshu.com/p/5d4ffe267596 CommandLineRunner或者ApplicationRunner接口
个人博客即将同步至腾讯云+社区,邀请你们一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=33hxmqpel3wgg