構建系統

強烈建議您選擇一個支援依賴管理並可以從 Maven Central 倉庫獲取構件的構建系統。我們推薦您選擇 Maven 或 Gradle。Spring Boot 也可以與其他構建系統(例如 Ant)一起使用,但支援程度不如 Maven 或 Gradle。

依賴管理

Spring Boot 的每個版本都提供了一個它所支援的精選依賴列表。實際上,您無需在構建配置中為這些依賴項指定版本,因為 Spring Boot 會為您管理。當您升級 Spring Boot 本身時,這些依賴項也會以一致的方式進行升級。

如果需要,您仍然可以指定版本並覆蓋 Spring Boot 的推薦設定。

該精選列表包含所有可與 Spring Boot 一起使用的 Spring 模組以及經過精煉的第三方庫列表。該列表以標準物料清單(spring-boot-dependencies)的形式提供,可用於 MavenGradle

每個 Spring Boot 版本都與 Spring Framework 的一個基礎版本相關聯。我們**強烈**建議您不要指定其版本。

Maven

要了解如何在 Maven 中使用 Spring Boot,請參閱 Spring Boot 的 Maven 外掛文件

Gradle

要了解如何在 Gradle 中使用 Spring Boot,請參閱 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.4.5" />

	<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 模組,請參閱“操作指南”中的不使用 spring-boot-antlib 從 Ant 構建可執行歸檔檔案部分。

啟動器

啟動器是一組方便的依賴描述符,您可以將其包含在您的應用程式中。您無需翻遍示例程式碼並複製貼上大量的依賴描述符,即可獲得所需的所有 Spring 及相關技術的一站式服務。例如,如果您想開始使用 Spring 和 JPA 進行資料庫訪問,只需在專案中包含 spring-boot-starter-data-jpa 依賴項即可。

啟動器包含您快速啟動和執行專案所需的大量依賴項,並提供一套一致、受支援的託管傳遞依賴項。

命名的含義

所有**官方**啟動器都遵循相似的命名模式:spring-boot-starter-*,其中 * 代表特定型別的應用程式。這種命名結構旨在幫助您查詢啟動器。許多 IDE 中的 Maven 整合允許您按名稱搜尋依賴項。例如,安裝相應的 Eclipse 或 Spring Tools 外掛後,您可以在 POM 編輯器中按下 ctrl-space 並輸入“spring-boot-starter”以獲取完整列表。

正如建立自己的啟動器部分所解釋的,第三方啟動器不應以 spring-boot 開頭,因為這是為官方 Spring Boot 構件保留的。相反,第三方啟動器通常以專案名稱開頭。例如,一個名為 thirdpartyproject 的第三方啟動器專案通常會被命名為 thirdpartyproject-spring-boot-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 和 Rabbit MQ 的啟動器

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 的啟動器

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 Reactive 和 Lettuce 客戶端訪問 Redis 鍵值資料儲存的啟動器

spring-boot-starter-data-rest

使用 Spring Data REST 和 Spring MVC 透過 REST 暴露 Spring Data 倉庫的啟動器

spring-boot-starter-freemarker

使用 FreeMarker 檢視構建 MVC Web 應用程式的啟動器

spring-boot-starter-graphql

使用 Spring GraphQL 構建 GraphQL 應用程式的啟動器

spring-boot-starter-groovy-templates

使用 Groovy 模板檢視構建 MVC Web 應用程式的啟動器

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 應用程式的啟動器。是 spring-boot-starter-web 的替代品

spring-boot-starter-jooq

使用 jOOQ 透過 JDBC 訪問 SQL 資料庫的啟動器。是 spring-boot-starter-data-jpaspring-boot-starter-jdbc 的替代品

spring-boot-starter-json

用於讀寫 JSON 的啟動器

spring-boot-starter-mail

使用 Java Mail 和 Spring Framework 郵件傳送支援的啟動器

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

使用 Spring for Apache Pulsar 的啟動器

spring-boot-starter-pulsar-reactive

使用 Spring for Apache Pulsar Reactive 的啟動器

spring-boot-starter-quartz

使用 Quartz 排程器的啟動器

spring-boot-starter-rsocket

構建 RSocket 客戶端和伺服器的啟動器

spring-boot-starter-security

使用 Spring Security 的啟動器

spring-boot-starter-test

用於使用 JUnit Jupiter、Hamcrest 和 Mockito 等庫測試 Spring Boot 應用程式的啟動器

spring-boot-starter-thymeleaf

使用 Thymeleaf 檢視構建 MVC Web 應用程式的啟動器

spring-boot-starter-validation

使用 Java Bean Validation 和 Hibernate Validator 的啟動器

spring-boot-starter-web

使用 Spring MVC 構建 Web(包括 RESTful)應用程式的啟動器。使用 Tomcat 作為預設的嵌入式容器

spring-boot-starter-web-services

使用 Spring Web Services 的啟動器

spring-boot-starter-webflux

使用 Spring Framework 響應式 Web 支援構建 WebFlux 應用程式的啟動器

spring-boot-starter-websocket

使用 Spring Framework MVC WebSocket 支援構建 WebSocket 應用程式的啟動器

除了應用程式啟動器外,還可以使用以下啟動器來新增生產就緒特性

表 2. Spring Boot 生產啟動器
名稱 描述

spring-boot-starter-actuator

用於使用 Spring Boot Actuator 的啟動器,它提供生產就緒特性來幫助您監控和管理應用程式

最後,如果您想排除或替換特定的技術方面,Spring Boot 還包括以下啟動器

表 3. Spring Boot 技術啟動器
名稱 描述

spring-boot-starter-jetty

使用 Jetty 作為嵌入式 Servlet 容器的啟動器。是 spring-boot-starter-tomcat 的替代品

spring-boot-starter-log4j2

使用 Log4j2 進行日誌記錄的啟動器。是 spring-boot-starter-logging 的替代品

spring-boot-starter-logging

使用 Logback 進行日誌記錄的啟動器。預設的日誌啟動器

spring-boot-starter-reactor-netty

使用 Reactor Netty 作為嵌入式響應式 HTTP 伺服器的啟動器。

spring-boot-starter-tomcat

使用 Tomcat 作為嵌入式 Servlet 容器的啟動器。由 spring-boot-starter-web 使用的預設 Servlet 容器啟動器

spring-boot-starter-undertow

使用 Undertow 作為嵌入式 Servlet 容器的啟動器。是 spring-boot-starter-tomcat 的替代品

要了解如何替換技術方面,請參閱有關替換 Web 伺服器日誌系統的操作指南文件。

有關社群貢獻的其他啟動器列表,請參閱 GitHub 上 spring-boot-starters 模組中的 README 檔案