Build Systems
强烈建议你选择一个支持 依赖管理 并且能够使用发布到 Maven Central 仓库的构件的构建系统。我们推荐你选择 Maven 或 Gradle。虽然也可以让 Spring Boot 与其他构建系统(例如 Ant)一起工作,但它们并没有得到特别好的支持。
依赖管理
Spring Boot 的每个版本都提供了一份经过精挑细选的依赖项列表,这些依赖项是它所支持的。在实践中,你无需在构建配置中为这些依赖项指定版本,因为 Spring Boot 会为你统一管理。当你升级 Spring Boot 本身时,这些依赖项也会以一致的方式随之升级。
如果需要,你仍然可以指定一个版本并覆盖 Spring Boot 的推荐配置。
每个 Spring Boot 版本都关联一个基础版本的 Spring Framework。我们强烈建议您不要指定其版本。
Maven
要了解如何将 Spring Boot 与 Maven 一起使用,请参阅 Spring Boot 的 Maven 插件文档:
Gradle
要了解如何将 Spring Boot 与 Gradle 结合使用,请参阅 Spring Boot 的 Gradle 插件文档:
Ant
可以使用 Apache Ant + Ivy 构建 Spring Boot 项目。spring-boot-antlib “AntLib” 模块也可用于帮助 Ant 创建可执行的 jar 包。
要声明依赖项,一个典型的 ivy.xml 文件看起来类似于以下示例:
<ivy-module version="2.0">
<info organisation="org.springframework.boot" module="spring-boot-sample-ant" />
<configurations>
<conf name="compile" description="everything needed to compile this module" />
<conf name="runtime" extends="compile" description="everything needed to run this module" />
</configurations>
<dependencies>
<dependency org="org.springframework.boot" name="spring-boot-starter"
rev="${spring-boot.version}" conf="compile" />
</dependencies>
</ivy-module>
一个典型的 build.xml 如下例所示:
<project
xmlns:ivy="antlib:org.apache.ivy.ant"
xmlns:spring-boot="antlib:org.springframework.boot.ant"
name="myapp" default="build">
<property name="spring-boot.version" value="3.5.10" />
<target name="resolve" description="--> retrieve dependencies with ivy">
<ivy:retrieve pattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
</target>
<target name="classpaths" depends="resolve">
<path id="compile.classpath">
<fileset dir="lib/compile" includes="*.jar" />
</path>
</target>
<target name="init" depends="classpaths">
<mkdir dir="build/classes" />
</target>
<target name="compile" depends="init" description="compile">
<javac srcdir="src/main/java" destdir="build/classes" classpathref="compile.classpath" />
</target>
<target name="build" depends="compile">
<spring-boot:exejar destfile="build/myapp.jar" classes="build/classes">
<spring-boot:lib>
<fileset dir="lib/runtime" />
</spring-boot:lib>
</spring-boot:exejar>
</target>
</project>
如果你不想使用 spring-boot-antlib 模块,请参阅“How-to Guides”中的 不使用 spring-boot-antlib 从 Ant 构建可执行归档文件 部分。
Starters
Starter 是一组便捷的依赖描述符,你可以将其包含在应用程序中。这样,你无需在示例代码中四处查找并复制粘贴大量依赖描述符,即可一站式获取所需的所有 Spring 及相关技术。例如,如果你想使用 Spring 和 JPA 进行数据库访问,只需在项目中引入 spring-boot-starter-data-jpa 依赖即可。
Starter 包含了大量依赖项,可帮助你快速启动并运行项目,并提供一组一致且受支持的托管传递依赖项。
以下应用启动器由 Spring Boot 在 org.springframework.boot 组下提供:
表 1. Spring Boot 应用启动器
| 名称 | 描述 |
|---|---|
spring-boot-starter | 核心启动器,包括自动配置支持、日志记录和 YAML |
spring-boot-starter-activemq | 使用 Apache ActiveMQ 的 JMS 消息传递入门示例 |
spring-boot-starter-amqp | Spring AMQP 和 RabbitMQ 入门示例 |
spring-boot-starter-aop | Spring AOP 与 AspectJ 的面向切面编程入门 |
spring-boot-starter-artemis | 使用 Apache Artemis 的 JMS 消息传递入门示例 |
spring-boot-starter-batch | Spring Batch 入门示例 |
spring-boot-starter-cache | 用于使用 Spring Framework 缓存支持的入门工具 |
spring-boot-starter-data-cassandra | 使用 Cassandra 分布式数据库和 Spring Data Cassandra 的入门指南 |
spring-boot-starter-data-cassandra-reactive | 使用 Cassandra 分布式数据库和 Spring Data Cassandra Reactive 的入门示例 |
spring-boot-starter-data-couchbase | 用于 Couchbase 面向文档数据库和 Spring Data Couchbase 的入门示例 |
spring-boot-starter-data-couchbase-reactive | 用于 Couchbase 面向文档数据库和 Spring Data Couchbase Reactive 的入门示例 |
spring-boot-starter-data-elasticsearch | 用于 Elasticsearch 搜索和分析引擎以及 Spring Data Elasticsearch 的入门工具 |
spring-boot-starter-data-jdbc | Spring Data JDBC 入门示例 |
spring-boot-starter-data-jpa | 使用 Spring Data JPA 与 Hibernate 的入门示例 |
spring-boot-starter-data-ldap | Spring Data LDAP 使用入门 |
spring-boot-starter-data-mongodb | 使用 MongoDB 面向文档数据库和 Spring Data MongoDB 的入门指南 |
spring-boot-starter-data-mongodb-reactive | 用于 MongoDB 面向文档数据库和 Spring Data MongoDB Reactive 的 Starter |
spring-boot-starter-data-neo4j | 使用 Neo4j 图数据库和 Spring Data Neo4j 的入门指南 |
spring-boot-starter-data-r2dbc | Spring Data R2DBC 入门示例 |
spring-boot-starter-data-redis | 使用 Spring Data Redis 和 Lettuce 客户端操作 Redis 键值数据存储的入门示例 |
spring-boot-starter-data-redis-reactive | 使用 Spring Data Redis 响应式编程和 Lettuce 客户端操作 Redis 键值数据存储的入门示例 |
spring-boot-starter-data-rest | 通过 Spring Data REST 和 Spring MVC 将 Spring Data 仓库以 REST 形式暴露的 Starter |
spring-boot-starter-freemarker | 用于使用 FreeMarker 视图构建 MVC Web 应用程序的 Starter |
spring-boot-starter-graphql | 用于使用 Spring GraphQL 构建 GraphQL 应用程序的入门模板 |
spring-boot-starter-groovy-templates | 用于使用 Groovy Templates 视图构建 MVC Web 应用程序的 Starter |
spring-boot-starter-hateoas | 基于 Spring MVC 和 Spring HATEOAS 构建超媒体 RESTful Web 应用程序的入门项目 |
spring-boot-starter-integration | Spring Integration 入门示例 |
spring-boot-starter-jdbc | 使用 HikariCP 连接池的 JDBC 入门示例 |
spring-boot-starter-jersey | 用于使用 JAX-RS 和 Jersey 构建 RESTful Web 应用程序的 Starter。可作为 spring-boot-starter-web 的替代方案。 |
spring-boot-starter-jooq | 用于通过 JDBC 使用 jOOQ 访问 SQL 数据库的 Starter。可作为 spring-boot-starter-data-jpa 或 spring-boot-starter-jdbc 的替代方案。 |
spring-boot-starter-json | 用于读写 JSON 的入门工具 |
spring-boot-starter-mail | 用于 Java Mail 和 Spring Framework 邮件发送支持的 Starter |
spring-boot-starter-mustache | 使用 Mustache 视图构建 Web 应用程序的入门模板 |
spring-boot-starter-oauth2-authorization-server | 用于使用 Spring Authorization Server 功能的入门项目 |
spring-boot-starter-oauth2-client | 用于使用 Spring Security 的 OAuth2/OpenID Connect 客户端功能的入门示例 |
spring-boot-starter-oauth2-resource-server | 用于使用 Spring Security 的 OAuth2 资源服务器功能的入门示例 |
spring-boot-starter-pulsar | 用于 Apache Pulsar 的 Spring 入门工具 |
spring-boot-starter-pulsar-reactive | 用于 Spring for Apache Pulsar Reactive 的 Starter |
spring-boot-starter-quartz | Quartz 调度器的入门示例 |
spring-boot-starter-rsocket | 用于构建 RSocket 客户端和服务器的 Starter |
spring-boot-starter-security | Spring Security 入门示例 |
spring-boot-starter-test | 用于测试 Spring Boot 应用程序的 Starter,包含 JUnit Jupiter、Hamcrest 和 Mockito 等库 |
spring-boot-starter-thymeleaf | 使用 Thymeleaf 视图构建 MVC Web 应用程序的入门模板 |
spring-boot-starter-validation | 使用 Hibernate Validator 进行 Java Bean Validation 的入门示例 |
spring-boot-starter-web | 用于构建 Web 应用(包括 RESTful 应用)的 Starter,基于 Spring MVC。默认使用 Tomcat 作为内嵌容器。 |
spring-boot-starter-web-services | Spring Web Services 入门 |
spring-boot-starter-webflux | 用于使用 Spring Framework 的 Reactive Web 支持构建 WebFlux 应用程序的 Starter |
spring-boot-starter-websocket | 使用 Spring Framework 的 MVC WebSocket 支持构建 WebSocket 应用程序的入门工具 |
除了应用 starters 之外,还可以使用以下 starters 来添加 生产就绪 功能:
表 2. Spring Boot 生产级 starters
最后,Spring Boot 还包含以下 starter,如果你想要排除或替换特定的技术方面,可以使用它们:
表 3. Spring Boot 技术 starters
| 名称 | 描述 |
|---|---|
spring-boot-starter-jetty | 用于使用 Jetty 作为嵌入式 Servlet 容器的 Starter。是 spring-boot-starter-tomcat 的替代方案 |
spring-boot-starter-log4j2 | 用于使用 Log4j2 进行日志记录的 Starter。是 spring-boot-starter-logging 的替代方案 |
spring-boot-starter-logging | 使用 Logback 进行日志记录的 Starter。默认的日志 Starter |
spring-boot-starter-reactor-netty | 用于使用 Reactor Netty 作为嵌入式响应式 HTTP 服务器的 Starter。 |
spring-boot-starter-tomcat | 用于使用 Tomcat 作为嵌入式 Servlet 容器的 Starter。spring-boot-starter-web 默认使用的 Servlet 容器 Starter |
spring-boot-starter-undertow | 用于使用 Undertow 作为嵌入式 Servlet 容器的 Starter。是 spring-boot-starter-tomcat 的替代方案 |
要了解如何更换技术组件,请参阅 更换 Web 服务器 和 日志系统 的操作指南文档。
有关更多社区贡献的 starter 列表,请参阅 GitHub 上 spring-boot-starters 模块中的 README 文件。