为了更好的学习Spring Boot和提升阅读英文材料的能力,试着翻译Spring Boot Reference Guide,估计会有很多翻译错误的地方。
Spring Boot Reference Guide
2.0.0.RELEASE
Using Spring Boot
本节将详细介绍如何使用Spring Boot。涵盖了build systems, auto-configuration以及如何运行应用程序等主题。我们还介绍了一些Spring Boot的最佳实例。尽管Spring Boot没有特殊之处(它只是你可以使用的另一个库),但有一些建议,如果遵循这些建议,你的开发过程会更容易一些。
1. Build System
can consume artifacts published to the "Maven Central" repository的build system。推荐选择Maven或者Gradle。虽然可以在别的build system(比如Ant)上使用Spring Boot,但是支持的不是特别好。
1.1 Dependency Management
Spring Boot的每个发行版本都提供了它所支持的a curated list of dependencies。实际上,你不需要为build configuration中的任何dependencies提供版本信息,因为Spring Boot替你管理这些。当你升级Spring Boot本身时,这些依赖关系也会相应升级。
Note: 需要的时候也可以指定版本信息来覆盖Spring Boot的推荐版本。
The curated list包含了可以在Spring Boot中使用的所有spring modules以及a refined list of第三方库。这个列表是可用于Maven和Gradle中的标准spring-boot-dependencies。
Warning:每个Spring Boot的版本与Spring Framework的基本版本相关联,强烈建议不要指定其版本。
2. Maven
Maven用户可以通过继承spring-boot-starter-parent来获得合理的默认配置。The parent project提供以下功能:
- java 1.8为默认编译器级别;
- UTF-8源码编码;
- A Dependency Management section,继承自spring-boot-dependencies pom,管理公共dependencies的版本;
- 智能的资源过滤;
- 智能的插件配置(exec plugin,Git commit ID,和 shade);
- 对application.properties和application.yml包括profile-specific files(例如application-dev.properties和application-dev.yml)的智能过滤;
注意,由于application.properties和application.yml文件使用Spring style placeholders(${...}),Maven过滤改变为使用@..@ placeholders。(可以通过设置名为resource.delimiter的Maven属性来覆盖它)。
Inheriting the Starter Parent
要将项目配置为从spring-boot-starter-parent继承,请按如下所示设置parent:
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
Note:只需在此依赖项上指定Spring Boot版本号, 如果导入额外的启动器,则可以安全地省略版本号。
你也可以通过覆盖自己项目中的属性来覆盖各个依赖项。 例如,要升级到另一个Spring Data发行版,可以将以下内容添加到pom.xml中:
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
Tip:Check the spring-boot-dependencies pom for a list of supported properties.
Using Spring Boot without the Parent POM
不是所有人都喜欢继承spring-boot-starter-parent POM。也许你有自己所需要的corporate standard parent,或者你可能更愿意明确声明所有Maven配置。如果不想使用spring-boot-starter-parent,你仍然可以通过使用scope=import dependency来保留dependency management(不是插件管理)的好处,如下所示:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
如上所述,上述示例的设置不会让你使用属性重写个别依赖关系。要达到相同的效果,需要在spring-boot-dependencies entry之前在项目的dependencyManagement中添加一个entry。例如,要升级到另一个Spring Data的发行版,可以将以下元素添加到你的pom.xml中:
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Note:
在前面的例子中,我们指定了一个BOM,其实任何依赖类型都可以用同样的方法重写。