SpringBoot 官方文檔中文版 - 2. 構建系統


前言

這是 SpringBoot 官方文檔中文翻譯版系列第二篇文章。

上一篇是:SpringBoot 官方文檔中文版 - 1. 入門指南

根據官網的順序,從構建系統,到將應用程序打包用於生產,是官網中 user.html 這一章的內容,因為篇幅較長,故拆分成若干章節。

使用 SpringBoot 進行開發

本節將更詳細地介紹如何使用 Spring Boot。它涵蓋了構建系統、自動配置以及如何運行應用程序等主題。我們還將介紹一些 Spring Boot 的最佳實踐。盡管 Spring Boot 沒有什么特別之處(它只是您可以使用的另一個庫),但是有一些建議,如果遵循這些建議,將使您的開發過程稍微容易一些。

如果您正在開始使用 Spring Boot,那么在深入本節之前,您可能應該閱讀入門指南

構建系統

強烈建議您選擇支持依賴項管理並能夠使用發布到 “Maven Central” 存儲庫的構件的構建系統。我們建議您選擇 Maven 或 Gradle。可以讓 Spring Boot 與其他構建系統(例如Ant)一起工作,但它們並沒有得到很好的支持。

1. 依賴管理

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

提示:如果需要的話,您仍然可以指定一個版本並覆蓋 Spring Boot 的建議。

這個列表包含了所有可以與 Spring Boot 一起使用的 Spring 模塊,以及一個改進的第三方庫列表。該列表作為標准的材料清單(spring-boot-dependencies)可用,可以與 Maven 和 Gradle 一起使用。

警告:Spring Boot 的每個版本都與 Spring 框架的一個基本版本相關聯。我們強烈建議您不要指定它的版本。

2. Maven

要了解如何使用 Spring Boot 與 Maven,請參閱 Spring Boot 的 Maven 插件的文檔:

3. Gradle

要了解如何在 Gradle 中使用 Spring Boot,請參考 Spring Boot 的 Gradle 插件的文檔:

4. 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="2.5.3" />

    <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 模塊,請參閱 howto.html " How-to "。

5. Starters

starters 是一組方便的依賴項描述符,您可以將其包含在應用程序中。您可以獲得所需的所有 Spring 和相關技術的一站式服務,而無需遍尋示例代碼和復制粘貼依賴描述符。例如,如果您想開始使用 Spring 和 JPA 進行數據庫訪問,請在項目中包含Spring -boot-start -data- JPA 依賴項。

starters 包含許多依賴項,您需要這些依賴項才能使項目快速啟動和運行,並具有一組一致的、受支持的托管傳遞依賴項。

所有官方的 starters 都遵循類似的命名模式:Spring-boot-starter -,其中是一種特殊類型的應用程序。這個命名結構旨在幫助您找到初學者。許多 ide 中的 Maven 集成允許您按名稱搜索依賴項。例如,安裝了適當的 Eclipse 或 Spring Tools 插件后,您可以在 POM 編輯器中按 ctrl-space 並輸入“Spring -boot-starter”來獲得完整的列表。

正如在“創建您自己的 starter”一節中解釋的那樣,第三方 starters 的名字不應該以 Spring - Boot 開頭,因為它是為官方 Spring Boot 工件保留的。相反,第三方 starters 通常以項目的名稱開始。例如,名為 thirdpartyproject 的第三方 starter 項目通常被命名為 thirdpartyproject-spring-boot-starter。

下面的 starters 是由 Spring Boot 在 org.springframework.boot 組下提供的:

表1: Spring Boot application starters

名稱 描述
spring-boot-starter 核心 starter,包括自動配置支持、日志記錄和 YAML
spring-boot-starter-activemq 使用 Apache ActiveMQ 的 JMS 消息傳遞 starter
spring-boot-starter-amqp 使用 Spring AMQP 和 Rabbit MQ 的 starter
spring-boot-starter-aop 使用 Spring AO P和 AspectJ 進行面向方面編程的 starter
spring-boot-starter-artemis 使用 Apache Artemis 的進行 JMS 消息傳遞的 starter
spring-boot-starter-batch 使用 Spring Batch 的 starter
spring-boot-starter-cache 使用 Spring 框架的緩存支持的 starter
spring-boot-starter-data-cassandra 使用 Cassandra 分布式數據庫和 Spring Data Cassandra 的 starter
spring-boot-starter-data-cassandra-reactive 使用 Cassandra 分布式數據庫和 Spring Data Cassandra Reactive 的 starter
spring-boot-starter-data-couchbase 使用 Couchbase 面向文檔的數據庫和 Spring Data Couchbase 的 starter
spring-boot-starter-data-couchbase-reactive 使用 Couchbase 面向文檔的數據庫和 Spring Data Couchbase Reactive 的 starter
spring-boot-starter-data-elasticsearch 使用 Elasticsearch 搜索和分析引擎和 Spring Data Elasticsearch 的 starter
spring-boot-starter-data-jdbc Starter for using Spring Data JDBC
spring-boot-starter-data-jpa Starter for using Spring Data JPA with Hibernate
spring-boot-starter-data-ldap Starter for using Spring Data LDAP
spring-boot-starter-data-mongodb Starter for using MongoDB document-oriented database and Spring Data MongoDB
spring-boot-starter-data-mongodb-reactive Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive
spring-boot-starter-data-neo4j Starter for using Neo4j graph database and Spring Data Neo4j
spring-boot-starter-data-r2dbc Starter for using Spring Data R2DBC
spring-boot-starter-data-redis 使用 Redis key-value 數據存儲與 Spring data Redis 和 Lettuce 客戶端
spring-boot-starter-data-redis-reactive 使用 Redis key-value 數據存儲與 Spring data Redis Reactive 和 Lettuce 客戶端
spring-boot-starter-data-rest Starter for exposing Spring Data repositories over REST using Spring Data REST
spring-boot-starter-groovy-templates Starter for building MVC web applications using Groovy Templates views
spring-boot-starter-hateoas Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS
spring-boot-starter-integration Starter for using Spring Integration
spring-boot-starter-jdbc 使用 JDBC 與 HikariCP 連接池
spring-boot-starter-jersey 使用 JAX-RS 和 Jersey 構建 RESTful web 應用程序的 starter。spring-boot-starter-web 的替代方案。
spring-boot-starter-jooq 使用 jOOQ 訪問 SQL 數據庫,可以替代 spring-boot-starter-data-jpa 或 spring-boot-starter-jdbc
spring-boot-starter-json Starter for reading and writing json
spring-boot-starter-jta-atomikos Starter for JTA transactions using Atomikos
spring-boot-starter-mail 使用 Java 郵件和 Spring 框架的電子郵件發送支持
spring-boot-starter-mustache Starter for building web applications using Mustache views
spring-boot-starter-oauth2-client 使用 Spring Security 的 OAuth2/OpenID 連接客戶端
spring-boot-starter-oauth2-resource-server Starter for using Spring Security’s OAuth2 resource server features
spring-boot-starter-quartz Starter for using the Quartz scheduler
spring-boot-starter-rsocket Starter for building RSocket clients and servers
spring-boot-starter-security Starter for using Spring Security
spring-boot-starter-test Starter for testing Spring Boot applications with libraries including JUnit Jupiter, Hamcrest and Mockito
spring-boot-starter-thymeleaf Starter for building MVC web applications using Thymeleaf views
spring-boot-starter-validation 使用 Hibernate 驗證器對 Java Bean 進行驗證
spring-boot-starter-web 使用 Spring MVC 構建 web,包括 RESTful 應用程序,使用 Tomcat 作為默認的嵌入式容器。
spring-boot-starter-web-services Starter for using Spring Web Services
spring-boot-starter-webflux 使用 Spring 框架的響應式 Web 支持構建 WebFlux 應用程序
spring-boot-starter-websocket 使用 Spring 框架的 WebSocket 支持來構建 WebSocket 應用程序。

表2: Spring Boot production starters

名稱 描述
spring-boot-starter-actuator 使用 Spring Boot’s Actuator,它提供了生產准備功能,以幫助您監視和管理您的應用程序。

最后,Spring Boot 還包括以下 starters,如果您想排除或交換特定的技術方面,可以使用它們:

表3: SpringBoot 技術 starters

名稱 描述
spring-boot-starter-jetty 初學者使用 Jetty 作為嵌入式 servlet 容器。可以替代 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。默認的 servlet 容器啟動器由 spring-boot-starter-web 使用。
spring-boot-starter-undertow 使用 Undertow 作為嵌入式 servlet 容器的 starter。可以替代 spring-boot-starter-tomcat

有關社區貢獻的其他 starters 列表,請參閱 GitHub 上 spring-boot-starter 模塊中的 README 文件

每天學習一點點,每天進步一點點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM