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 文件

每天学习一点点,每天进步一点点。

posted @ 2021-07-23 15:03  爱吃西瓜的番茄酱  阅读(3588)  评论(0编辑  收藏  举报