Spring Boot中文手册(2):使用Spring Boot
“ 该系列的文章主要参考Spring Boot的官方文档来进行翻译,逐步将官方文档翻译过来,方便大家共同学习和讨论,但是毕竟能力有限,如有问题,感谢大家留言指正。”
本章将更深入地带大家了解如何使用Spring Boot的细节。包括构建系统、自动配置以及如何启动你的应用。同时也会给大家介绍一些Spring Boot的最佳实践。虽然Spring Boot并没有什么特别与众不同的地方(只是另一个你能想到的java类库),但是如果你在开发的过程中借鉴这些建议,那么你将省去不少功夫。
如果你将要开始使用Spring Boot进行开发了,那么我建议你在开始这一章节的学习之前,可以看一下上一篇文章Spring Boot中文手册(1):你的第一个Spring Boot应用!
1 构建系统
强烈推荐选择构建工具的时候,优先考虑支持依赖管理并且可以发布你的组件到“Maven Central”仓库的构建管理工具,比如Maven或者Gradle。当然也可以使用其他的构建管理工具来搭建Spring Boot项目,比如Ant,但是我们并不是非常建议你这么做。
1.1 依赖管理
每次Spring Boot发布时,都会提供一个它支持的依赖清单列表,在实际使用的时候,就不需要给你构建配置中的每一个依赖都分别定义版本号,全部交给Spring Boot来管理就可以,这些依赖也会在每次迭代过程中升级。
如果有必要,你仍然可以指定依赖的版本,来覆盖掉Spring Boot的建议版本。
当你使用Spring Boot搭建项目时,它的依赖清单中包含的所有Spring组件同你从第三方类库那里提炼出来的依赖清单是相同的。这是一个标准的物料清单 (spring-boot-dependencies
) ,无论是用Maven还是Gradle,都可以构建你的项目。
每一版Spring Boot都与一个特定版本的Spring Framework相关联,我们强烈建议你不要修改这个版本。
1.2 Maven
使用Maven构建的话,你可以通过继承 spring-boot-starter-parent
项目来获取合适的默认配置。这个项目提供了一下特性:
-
使用Java 1.8作为默认编译级别
-
编码格式为UTF-8
-
一个依赖管理的节点,继承于spring-boot-dependencies pom文件,控制普通依赖的版本,当你在自己的pom文件中引入这些依赖的时候,可以省略掉版本号,不用明确指出标签。
-
带有执行ID的一个执行goal,
repackage
。 -
合适的资源过滤配置
-
合适的插件配置(比如exec插件,Git Commit ID,shade)。
-
针对于带有指定profile配置信息的
application.properties
和application.yml
的资源文件过滤,比如application-dev.properties
或者application-dev.yml
。
注意: application.properties
和 application.yml
支持Spring 风格的占位符 ${…}
,Maven的filtering使用的占位符被修改为 @[email protected]
。(你也可以通过设置Maven属性 resource.delimiter
来覆盖它。)
1.2.1 继承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.1.2.RELEASE</version>
</parent>
在这里,你指需要制定Spring Boot的版本号就可以了,当你引入其他starters的时候,就可以忽略掉版本号直接引入了。
在这个基础之上,你也可以在自己的工程中通过设置一个属性来覆盖依赖的版本。比如,想已升级到另一个Spring Data的发行版,你可以在 pom.xml
中增加如下配置:
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
可以通过spring-boot-dependencies这个组件的pom文件来查看依赖列表
1.2.2 不借助父POM来使用Spring Boot
并不是每个人都喜欢继承 spring-boot-starter-parent
这个pom。你可能被强迫使用公司级别的父POM,或者你自己本身就喜欢显式地声明你的Maven配置。
如果你并不想使用 spring-boot-starter-parent
,通过在POM文件中增加一个scope=import
依赖,你仍然可以体验到依赖管理带来的好处,像如下配置:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
这种配置就不允许你通过使用property配置来覆盖依赖的版本了,和继承父POM的那种情况是不一样的。为了达到相同的效果,你需要在配置文件的 spring-boot-dependencies
节点前,增加一个 dependencyManagement
节点,来使用你想要的那个版本jar包。比如,你想使用另外一个版本的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.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在上面的例子中,我们定义了一组jar包依赖集合,实际上任何类型的依赖都可以通过这种方式来覆盖版本号。
1.2.3 使用Spring Boot的Maven插件
Spring Boot中有一个可以将项目打包成可执行jar包的Maven插件。如果你想使用的话,只需要再你工程配置文件的 <plugins>
内添加这个插件即可,就如同下面这个例子中配置的一样:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果使用Spring Boot starter parent pom,你只需要添加该插件就可以了。如果你想改变他在parent中的定义,可能还需要进行一些配置。
1.3 Gradle
通过Gradle来使用Spring Boot的教程不在本手册中列出,在官方是通过单独的手册来提供的,官方有文档地址,大家去官网这个章节找到地址来查看相关内容,如果有时间的话,会把那个文档也翻译一遍给大家看。
1.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.1.2.RELEASE" />
<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手册的另外一章,“不使用spring-boot-antlib组件时,如何通过Ant构建一个可执行文件”,该章内容会在后续的文章中给出。
END