Introduction to Spring Boot CLI – Spring Boot CLI简介

最后修改: 2018年 4月 12日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

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

3.常见的终端命令

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:

例如,我们可以通过输入显示当前运行的CLI的版本

version

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

4.基本的Groovy脚本

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.

对多脚本应用程序的支持通常需要额外的构建工具,如MavenGradle

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.

关于所有支持Spring的Groovy注释的列表,请查看官方的docs

4.1. @Grab

4.1. @Grab

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

@Grab注解和Groovy的Java风格的import条款可以实现轻松的依赖管理和注入

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:

让我们来看看如何使用@Grab注解。

package org.test

@Grab("spring-boot-starter-actuator")

@RestController
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.

正如我们所看到的,spring-boot-starter-actuator是预配置的,允许简洁的脚本部署,不需要定制的应用程序或环境属性、XML或其他程序性配置,尽管这些东西都可以在必要时指定。

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

@Grab参数的完整列表–每个参数指定一个要下载和导入的库–可在这里获得。

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:

例如,我们可以使用熟悉的@Controller@Service注解来快速搭建一个标准的MVC控制器和服务

@RestController
class Example {
 
    @Autowired
    private MyService myService;

    @GetMapping("/")
    public String helloWorld() {
        return myService.sayWorld();
    }
}

@Service
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

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注释。

package bael.security

@Grab("spring-boot-starter-security")

@RestController
class SampleController {

    @RequestMapping("/")
    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

4.4.@测试

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

为了设置一个简单的JUnit测试,我们可以添加@Grab(‘junit’)@Test注释。

package bael.test

@Grab('junit')
class Test {
    //...
}

This will allow us to execute JUnit tests easily.

这将使我们能够轻松地执行JUnit测试。

4.5. DataSource and JdbcTemplate

4.5.DataSourceJdbcTemplate

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

可以指定持久性数据选项,包括DataSourceJdbcTemplate,而无需明确使用@Grabannotation

package bael.data

@Grab('h2')
@Configuration
@EnableWebMvc
@ComponentScan('bael.data')
class DataConfig {

    @Bean
    DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
          .setType(EmbeddedDatabaseType.H2).build();
    }

}

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

5.自定义配置

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 application.properties

Spring Boot将自动搜索/config目录中的application.ymlapplication.properties

├── 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.

应用程序属性的完整列表可以在Spring网站上找到这里

6. Conclusion

6.结论

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.

和往常一样,本文的源代码可以在GitHub上找到