49.2.1 远程shell命令

你可以使用Groovy或Java编写其他的shell命令(具体参考CRaSH文档),Spring Boot默认会搜索以下路径的命令:

  • classpath*:/commands/**

  • classpath*:/crash/commands/**

设置shell.command-path-patterns属性可以改变搜索路径。 如果使用可执行存档(archive),shell依赖的所有类都必须打包进一个内嵌的jar,而不是直接打包进可执行jar或war。

下面是一个从src/main/resources/commands/hello.groovy加载的'hello'命令:

package commands

import org.crsh.cli.Usage
import org.crsh.cli.Command

class hello {

    @Usage("Say Hello")
    @Command
    def main(InvocationContext context) {
        return "Hello"
    }

}

Spring Boot为InvocationContext添加一些其他属性,你可以在命令中访问它们:

属性名称

描述

spring.boot.version

Spring Boot的版本

spring.version

Spring核心框架的版本

spring.beanfactory

获取Spring的BeanFactory

spring.environment

获取Spring的Environment

Last updated