> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v4/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v4/untitled-14/untitled-2.md).

# 21.3　组合使用 Groovy 与 Spring Boot CLI

Groovy 编程语言要比 Java 简单得多。它的语法允许有一些快捷方式，比如省略分号和 public 关键词。同时，Groovy 类中的属性不像 Java 那样需要 Setter 和 Getter 方法。当然，Groovy 还有其他的一些属性，能够消除 Java 代码中很多的繁文缛节。

如果你愿意使用 Groovy 编写应用代码并通过 Spring Boot CLI 运行的话，那么 Spring Boot 能够借助 Groovy 的简洁性进一步简化 Spring 应用。为了阐述这一点，我们使用 Groovy 来重新编写 Contacts 应用程序。

为什么不呢？在初始版本的应用中，我们只有几个小的 Java 类，因此使用 Groovy 进行重写也没有太多的工作量。我们可以重用相同的 Thymeleaf 模板和 schema.sql 文件。既然我宣称 Groovy 能够进一步简化 Spring，那重写应用也不是什么大事儿。

在这个过程中，我们还会移除一些代码。Spring Boot CLI 本身就是启动器，所以不再需要前面所创建的 Application 类。Maven 和 Gradle 构建文件也不再需要了，因为我们将会通过 CLI 运行未编译的 Groovy 文件。少了 Maven 和 Gradle 之后，项目的整体结构将会变得更加扁平化，新的项目结构将会如下所示：

![](/files/-Lo-n1UiWxWY5qMM7wBb)

schema.sql、style.css 和 home.html 将会保持原样，但是需要将 Java 类转换为 Groovy。我们先从使用 Groovy 编写 Web 层开始。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://potoyang.gitbook.io/spring-in-action-v4/untitled-14/untitled-2.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
