This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly special about Spring Boot (it is just another library that you can consume), there are a few recommendations that, when followed, make your development process a little easier.
It is strongly recommended that you choose a build system that supports dependency management and that can consume artifacts published to the “Maven Central” repository. We would recommend that you choose Maven or Gradle. It is possible to get Spring Boot to work with other build systems (Ant, for example), but they are not particularly well supported.
Each release of Spring Boot provides a curated list of dependencies that it supports. In practice, you do not need to provide a version for any of these dependencies in your build configuration, as Spring Boot manages that for you. When you upgrade Spring Boot itself, these dependencies are upgraded as well in a consistent way.
Spring Boot的每个发行版都提供了一个它支持的依赖关系的策划列表。基于实践,在您的构建配置中您不需要为任何依赖提供一个版本。因为Spring Boot为您管理这些版本。当您升级Spring Boot本身的时候,这些依赖版本会以一致的方式很好的自动升级。
1 2
You can still specify a version and override Spring Boot’s recommendations if you need to do so.
如果您愿意的话,您依然可以指定一个版本覆盖Spring Boot建议的版本。
1 2 3 4
The curated list contains all the spring modules that you can use with Spring Boot as well as a refined list of third party libraries. The list is available as a standard Bills of Materials (spring-boot- dependencies) that can be used with both Maven and Gradle.
• Java 1.8 as the default compiler level. • UTF-8 source encoding. • A Dependency Management section, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit <version> tags for those dependencies when used in your own pom. • Sensible resource filtering. • Sensible plugin configuration (exec plugin, Git commit ID, and shade). • Sensible resource filtering for application.properties and application.yml including profile-specific files (for example, application-dev.properties and application-dev.yml)
Note that, since the application.properties and application.yml files accept Spring style placeholders (${… }), the Maven filtering is changed to use @..@placeholders. (You can override that by setting a Maven property called resource.delimiter.)
To configure your project to inherit from the spring-boot-starter-parent, set the parent as follows:
配置您的工程去继承spring-boot-starter-parent,设置父pom如下:
1 2 3 4 5 6
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.BUILD-SNAPSHOT</version> </parent>
1 2 3
You should need to specify only the Spring Boot version number on this dependency. If you import additional starters, you can safely omit the version number.
With that setup, you can also override individual dependencies by overriding a property in your own project. For instance, to upgrade to another Spring Data release train, you would add the following to your pom.xml:
13.2.2 Using Spring Boot without the Parent POM(不使用父POM)
1 2 3
Not everyone likes inheriting from the spring-boot-starter-parent POM. You may have your own corporate standard parent that you need to use or you may prefer to explicitly declare all your Maven configuration.
If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the dependency management (but not the plugin management) by using a scope=import dependency, as follows:
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.0.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
1 2 3 4 5 6
The preceding sample setup does not let you override individual dependencies by using a property, as explained above. To achieve the same result, you need to add an entry in the dependencyManagement of your project before the spring-boot-dependencies entry. For instance, to upgrade to another Spring Data release train, you could add the following element to your pom.xml:
如上所述,上述示例设置不会让您通过使用属性覆盖个人依赖关系。为了达到同样目标,您需要在dependencyManagement中的spring-boot-dependencies前使用一个节点。举个例子,升级另一个Spring Data 发行版本,您可以增加如下的元素到您的pom.xml中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<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.1.0.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
1 2
In the preceding example, we specify a BOM, but any dependency type can be overridden in the same way.
在之前的例子中,我们定义了一个清单,但是任何的依赖类型都能够以这种方式进行覆盖。
13.2.3 Using the Spring Boot Maven Plugin
1 2 3
Spring Boot includes a Maven plugin that can package the project as an executable jar. Add the plugin to your <plugins> section if you want to use it, as shown in the following example:
Spring Boot引入了一个Maven的插件,可以打包工程,使之成为一个可执行jar。如果您想使用它的话,增加该插件到部分。如下展示的示例:
If you use the Spring Boot starter parent pom, you need to add only the plugin. There is no need to configure it unless you want to change the settings defined in the parent.