Introduction to Spring Boot CLI – Spring Boot CLI简介

最后修改: 2018年 4月 12日

1. Introduction


Spring Boot CLI is a command-line abstraction that allows us to easily run Spring micro-services expressed as Groovy scripts. It also provides simplified and enhanced dependency management for those services.

Spring Boot CLI是一个命令行抽象,允许我们轻松地运行以Groovy脚本表达的Spring微服务。它还为这些服务提供了简化和增强的依赖性管理。

This short article takes a quick look at how to configure Spring Boot CLI and execute simple terminal commands to run pre-configured micro-services.

这篇短文简要介绍了如何配置Spring Boot CLI并执行简单的终端命令以运行预先配置的微服务

We’ll use Spring Boot CLI 2.0.0.RELEASE for this article. The newest version of Spring Boot CLI can be found over at Maven Central.

我们将在本文中使用Spring Boot CLI 2.0.0.RELEASE。最新版本的Spring Boot CLI可以在Maven Central找到。

2. Setting Up Spring Boot CLI

2.设置Spring Boot CLI

One of the easiest ways to set up Spring Boot CLI is to use SDKMAN. Setup and installation instructions for SDKMAN can be found here.

设置Spring Boot CLI的最简单方法之一是使用SDKMAN。SDKMAN的设置和安装说明可在此处找到。

After installing SDKMAN, run the following command to automatically install and configure Spring Boot CLI:

安装SDKMAN后,运行以下命令,自动安装和配置Spring Boot CLI。

$ sdk install springboot

To verify the install, run the command:


$ spring --version

We can also install Spring Boot CLI by compiling from source, and Mac users can use pre-built packages from Homebrew or MacPorts. See the official docs for all installation options.

我们还可以通过从源代码编译来安装Spring Boot CLI,Mac用户可以使用来自HomebrewMacPorts的预建包。请参阅官方的文档了解所有的安装选项。

3. Common Terminal Commands


Spring Boot CLI provides several useful commands and features out-of-the-box. One of the most helpful features is Spring Shell, which wraps commands with the necessary spring prefix.

Spring Boot CLI提供了一些开箱即用的有用命令和功能。最有用的功能之一是Spring Shell,它用必要的spring前缀来包装命令。

To start the embedded shell, we run:


spring shell

From here, we can directly enter desired commands without pre-pending the spring keyword (since we’re now in spring shell).

从这里,我们可以直接输入所需的命令,而不需要预留spring关键字(因为我们现在是在spring shell中)。

For example, we can display the current version of the running CLI by typing:



One of the most important commands is telling Spring Boot CLI to run a Groovy script:

最重要的命令之一是告诉Spring Boot CLI运行一个Groovy脚本:

run [SCRIPT_NAME].groovy

Spring Boot CLI will either automatically infer the dependencies or will do so given the correctly supplied annotations. After this, it will launch an embedded web container and app.

Spring Boot CLI会自动推断出依赖关系,或者根据正确提供的注解来推断。在这之后,它将启动一个嵌入式Web容器和应用程序。

Let’s take a closer look at how to use Groovy script with Spring Boot CLI!

让我们仔细看看如何在Spring Boot CLI中使用Groovy脚本!

4. Essential Groovy Scripts


Groovy and Spring come together with Spring Boot CLI to allow powerful, performant micro-services to be quickly scripted in single-file Groovy deployments.

Groovy和Spring与Spring Boot CLI结合在一起,允许在单文件Groovy部署中快速编写强大、高性能的微服务

Support for multiply-scripted applications usually requires additional build tools like Maven or Gradle.


Below we’ll cover some of the most common use-cases for Spring Boot CLI, reserving more complex setups for other articles.

下面我们将介绍Spring Boot CLI的一些最常见的使用情况,把更复杂的设置保留给其他文章。

For a list of all Spring-supported Groovy annotations, please check out the official docs.


4.1. @Grab

4.1. @Grab

The @Grab annotation and Groovy’s Java-esque import clauses allow for easy dependency management and injection.


In fact, most annotations abstract, simplify, and automatically include the necessary import statements. This allows us to spend more time thinking about architecture and the underlying logic of the services we want to deploy.


Let’s take a look at how to use the @Grab annotation:


package org.test


class ExampleRestController{

As we can see, spring-boot-starter-actuator comes pre-configured allowing for succinct script deployment without requiring a customized application or environmental properties, XML, or other programmatic configuration, though each of those things can be specified when necessary.


The full list of @Grab arguments — each specifying a library to download and import — is available here.


4.2. @Controller, @RestController, and @EnableWebMvc

4.2.@Controller, @RestController, @EnableWebMvc

To further expedite deployment, we can alternatively utilize Spring Boot CLI’s provided “grab hints” to automatically infer correct dependencies to import.

为了进一步加快部署,我们可以选择利用Spring Boot CLI提供的 “抓取提示 “来自动推断要导入的正确依赖关系

We’ll go over some of the most common use cases below.


For example, we can use the familiar @Controller and @Service annotations to quickly scaffold a standard MVC controller and service:


class Example {
    private MyService myService;

    public String helloWorld() {
        return myService.sayWorld();

class MyService {
    public String sayWorld() {
        return "World!";

Spring Boot CLI supports all default configuration for Spring Boot. So, we can our Groovy apps will automatically access static resources from their usual default locations.

Spring Boot CLI支持Spring Boot的所有默认配置。因此,我们的Groovy应用程序将自动从其通常的默认位置访问静态资源。

4.3. @EnableWebSecurity


To add Spring Boot Security options to our app, we can use the @EnableWebSecurity annotation, which will then be automatically downloaded by Spring Boot CLI.

为了向我们的应用程序添加Spring Boot安全选项,我们可以使用@EnableWebSecurity注解,然后由Spring Boot CLI自动下载。

Below, we’ll abstract part of this process using the spring-boot-starter-security dependency, which leverages the @EnableWebSecurity annotation under the hood:

下面,我们将使用 spring-boot-starter-security 依赖关系来抽象出这一过程的一部分,该依赖关系利用了@EnableWebSecurity注释。



class SampleController {

    public def example() {
        [message: "Hello World!"]

For more details on how to protect resources and handle security, please check out the official documentation.


4.4. @Test


To set up a simple JUnit test, we can add the @Grab(‘junit’) or @Test annotations:


package bael.test

class Test {

This will allow us to execute JUnit tests easily.


4.5. DataSource and JdbcTemplate


Persistent data options can be specified including DataSource or JdbcTemplate without explicitly using the @Grab annotation:



class DataConfig {

    DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()


By simply using familiar Spring bean configuration conventions, we’ve grabbed the H2 embedded database and set it as the DataSource.

通过简单地使用熟悉的Spring Bean配置约定,我们抓住了H2嵌入式数据库并将其设置为DataSource

5. Custom Configuration


There are two primary ways to configure a Spring Boot micro-service using Spring Boot CLI:

使用Spring Boot CLI配置Spring Boot微服务有两种主要方式。

  1. we can add argument parameters to our terminal commands
  2. we can use a customized YAML file to provide an application configuration

Spring Boot will automatically search the /config directory for application.yml or

Spring Boot将自动搜索/config目录中的

├── app
    ├── app.groovy
    ├── config
        ├── application.yml

We can also set up:


├── app
    ├── example.groovy
    ├── example.yml

A full list of application properties can be found here at Spring.


6. Conclusion


This concludes our quick walk-through of Spring Boot CLI! For more details, check out the official docs.

我们对Spring Boot CLI的快速演练到此结束!欲了解更多详情,请查看官方的文档

And as usual, the source code for this article can be found over on GitHub.