> 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-3.md).

# 21.2　使用 Spring Boot 构建应用

在本章剩余的内容中，我将会向你展现如何使用 Spring Boot 构建完整且符合现实要求（real-world）的应用程序。当然，“符合现实要求” 的定义标准会有些争议，对它的讨论超出了本章的范围。因此，与其在这里说构建符合现实要求的应用，还不如后退一步，说成我们所构建的应用程序比现实要求稍差一点，但是它能够代表使用 Spring Boot 所构建的更大型应用。

我们的应用是一个简单的联系人列表。它允许用户输入联系人信息（名字、电话号码以及 Email 地址），并且能够列出用户之前输入的所有联系人信息。

你可以自由选择使用 Maven 还是 Gradle 来构建应用程序，我更喜欢 Gradle，但是如果你喜欢 Maven 的话，我也将会列出所需的 Maven 代码。如下的程序清单展现了起始的 build.gradle 文件。开始的时候，依赖部分是空的，但是在这个过程中，我们将会使用依赖填充这部分的内容。

{% code title="程序清单 21.1 Contacts 应用所需的 Gradle 构建文件" %}

```
buildscript {
  repositories {
    mavenLocal()
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.4.RELEASE")
  }
}
  
apply plugin: 'java'
apply plugin: 'spring-boot'
jar {
  baseName = 'contacts'
  version = '0.1.0'
}
 
repositories {
  mavenCentral()
}
  
dependencies {
}
  
task wrapper(type: Wrapper) {
  gradleVersion = '1.8'
}
```

{% endcode %}

注意，构建中包含对 Spring Boot Gradle 的 buildscript 依赖。稍后将会看到，这会帮助我们生成一个可执行的超级 JAR 文件（uber- JAR），这个文件中将会包含应用的所有依赖。

如果你更喜欢 Maven 的话，如下的程序清单展现了完整的 pom.xml 文件。

{% code title="程序清单 21.2 Contacts 应用所需的 Maven 构建文件" %}

```markup
<?xml versions"1.0" encodings"UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsis"http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
      http://maven.apache.org/POM/4.0.0
      http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.habuma</groupId>
    <artifactId>contacts</artifactId>
    <version>0.1.0</version>
    <packaging>jar</packaging>
    
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactld>spring-boot-starter-parent</artifactld>
      <version>l.1.4.RELEASE</version>
    </parent>
    
    <dependencies>
    </dependencies>
    
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupld>
         <artifactId>spring-boot~maven-plugin</artifactId>
       </plugin>
     </plugins>
   </build>
</project>
```

{% endcode %}

与 Gradle 构建类似，这个 Maven 的 pom.xml 文件使用了 Spring Boot Maven 插件。这个 Maven 中的插件对应于 Gradle 插件，能够生成可执行的超级 JAR 文件。

同样需要注意的是，与 Gradle 构建不同，Maven 构架有一个 parent 项目。我们让项目的 Maven 构建基于 Spring Boot starter parent，这样的话，我们就能受益于 Maven 的依赖管理功能，对于项目中的很多依赖，就没有必要明确声明版本号了，因为版本号会从 parent 中继承得到。

按照 Maven 和 Gradle 项目的标准结构，完成后项目将会如下所示：

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

不要担心现在缺失 Java 和其他的资源文件。在开发 Contacts 应用的过程中，我们将会在下面的几个小节中创建这些文件，首先将会从构建应用的 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-3.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.
