一、构建系统
强烈推荐您选择一个支持依赖管理 的构建系统, 还可以将artifacts发布到“Maven Central”仓库。我们建议您选择Maven或者Gradle。虽然可以让Spring Boot与其它构建系统(如Ant)配合工作,但它们不会得到特别好的支持。
1.1 依赖管理
每一个版本的SpringBoot提供了一个它所支持的依赖内置清单。实际上,您不需要为构建配置提供任何版本的依赖,因为Spring Booot正在为您管理。当您升级Spring Boot时,这些依赖也将以一致的方式进行升级。
如果您觉得有必要,您仍然可以指定一个版本并覆盖Spring Boot的推荐。
管理的列表中包含可以使用Spring Boot的所有Spring模块以及第三方库的精简列表。 该列表可作为标准的物料(Materials)清单(spring-boot-dependencies)使用,并且还提供了对 Maven 和 Gradle 的额外支持。
Spring Boot的每个版本与Spring Framework的基本版本相关联,因此我们强烈建议您不要自己指定其版本。
二、Maven
Maven用户可以从spring-boot-starter-parent-parent 项目中继承,以获得合理的默认值。父项目提供以下功能:
- (Spring Boot 2.x)Java 1.8作为默认编译器级别。(Spring Boot 1.x)Java 1.6作为默认编译器级别。
- 源代码UTF-8编码。
- 依赖关系管理,允许您省略常见依赖的标签,其默认版本继承自spring-boot-dependencies POM。
- 更合理的资源过滤。
- 更合理的插件配置(exec plugin,surefire,Git commit ID,shade)。
- 针对application.properties和application.yml的更合理的资源过滤,包括特定的文件(例如application-foo.properties和application-foo.yml)
- 最后一点:由于默认的配置文件接受Spring样式占位符(${…}),Maven过滤更改为使用 @..@ 占位符(您可以使用Maven属性resource.delimiter覆盖它)。
2.1 继承启动器parent
要将项目配置为继承spring-boot-starter-parent,只需设置标签如下:
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
您只需要在此依赖上指定Spring Boot版本号。如果您导入其它的starter,则可以安全地省略版本号。
通过该设置,您还可以通过覆盖自己项目中的配置属性来覆盖单个依赖。例如,要升级到另一个Spring Data发行版本,您需要将以下内容添加到您的 pom.xml
文件中。
<properties>
<spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version>
</properties>
检查 spring-boot-dependencies
的pom 以获取受支持的属性列表。
2. 2 使用没有父POM的Spring Boot
不是每个人都喜欢从 spring-boot-starter-parent
继承POM配置。您可能需要使用自己公司标准的父POM,或者您可能只是希望明确地声明所有的Maven配置。
如果您不想使用 spring-boot-starter-parent
,则仍然可以通过使用 scope=import
依赖来维持依赖管理(但不是插件管理)的好处:
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
该设置不允许您使用如上属性来覆盖单个依赖。要实现相同的效果,您需要在您项目的 dependencyManagement
配置项中的 spring-boot-dependencies
依赖配置之前 添加配置。比如,要升级到另一个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>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
以上示例中,我们指定了一个 BOM,但是任何的依赖类型都可以用这个方法来覆盖。
2.3 变更Java版本
spring-boot-starter-parent
选择比较保守的Java兼容性。如果您想听取我们的建议使用更高的Java版本,您可以添加 java.version
属性:
<properties>
<java.version>1.8</java.version>
</properties>
2.4 使用Spring Boot Maven插件
Spring Boot包括了一个 Maven插件,它可以将项目打包成一个可执行jar。如果要使用它,请将插件添加到您的 <plugins>
中:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果您使用Spring Boot starter的父pom,则只需要添加插件。除非您要修改父级中定义的设置,否则不需要进行配置。
三、Gradle
Gradle用户可以直接在其依赖关系部分导入“启动器”。不像Maven,没有“超级父”导入来共享一些配置。
repositories {
jcenter()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:1.5.10.RELEASE")
}
spring-boot-gradle-plugin也是可用的,它提供了从源代码创建可执行jar并运行项目的任务。它还提供依赖关系管理,除其他功能外,还允许您省略由Spring Boot管理的任何依赖关系的版本号:
plugins {
id 'org.springframework.boot' version '1.5.10.RELEASE'
id 'java'
}
repositories {
jcenter()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
四、Ant
可以使用ApacheAnt + Ivy构建SpringBoot项目。 spring-boot-antlib“AntLib”模块也可用于帮助Ant创建可执行文件。
要声明依赖关系,典型的ivy.xml文件将如下所示:
<ivy-moduleversion="2.0">
<infoorganisation="org.springframework.boot"module="spring-boot-sample-ant" />
<configurations>
<confname="compile"description="everything needed to compile this module" />
<confname="runtime"extends="compile"description="everything needed to run this module" />
</configurations>
<dependencies>
<dependencyorg="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">
<propertyname="spring-boot.version"value="1.5.10.RELEASE" />
<targetname="resolve"description="--> retrieve dependencies with ivy">
<ivy:retrievepattern="lib/[conf]/[artifact]-[type]-[revision].[ext]" />
</target>
<targetname="classpaths"depends="resolve">
<pathid="compile.classpath">
<filesetdir="lib/compile"includes="*.jar" />
</path>
</target>
<targetname="init"depends="classpaths">
<mkdirdir="build/classes" />
</target>
<targetname="compile"depends="init"description="compile">
<javacsrcdir="src/main/java"destdir="build/classes"classpathref="compile.classpath" />
</target>
<targetname="build"depends="compile">
<spring-boot:exejardestfile="build/myapp.jar"classes="build/classes">
<spring-boot:lib>
<filesetdir="lib/runtime" />
</spring-boot:lib>
</spring-boot:exejar>
</target>
</project>
请参见第84.10节“从Ant构建可执行存档,而不使用spring-boot-antlib”如果不想使用spring-boot-antlib模块,请参阅“操作方法”。
五、Starter(启动器)
启动器是一组方便的依赖关系描述符,可以包含在应用程序中。您可以获得所需的所有Spring和相关技术的一站式服务,无需通过示例代码搜索和复制粘贴依赖配置。例如,如果要开始使用Spring和JPA进行数据库访问,那么只需在项目中包含spring-boot-starter-data-jpa依赖关系即可。
启动器包含许多依赖关系,包括您需要使项目快速启动并运行,并具有一致的受支持的依赖传递关系。
名字的含义(What’s in a name)
所有正式起动器都遵循类似的命名模式: spring-boot-starter- * ,其中 * 是特定类型的应用程序。这个命名结构旨在帮助你快速找到一个启动器。许多IDE中的Maven插件允许您按名称搜索依赖项。例如,安装Eclipse或STS的Maven插件后,您可以简单地在POM编辑器中点击 Dependency Hierarchy,并在filter输入“spring-boot-starter”来获取完整的列表。
如创建自己的启动器部分所述,第三方启动程序不应该从Spring-boot开始,因为它是为正式的SpringBoot artifacts 保留的。 acme 的第三方启动器通常被命名为acme-spring-boot-starter。
Spring Boot在org.springframework.boot组下提供了以下应用程序启动器:
表13.1.Spring Boot应用程序启动器
Name |
Description |
Pom |
Core starter, including auto-configuration support, logging and YAML |
||
Starter for JMS messaging using Apache ActiveMQ |
||
Starter for using Spring AMQP and Rabbit MQ |
||
Starter for aspect-oriented programming with Spring AOP and AspectJ |
||
Starter for JMS messaging using Apache Artemis |
||
Starter for using Spring Batch |
||
Starter for using Spring Framework’s caching support |
||
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku |
||
Starter for using Cassandra distributed database and Spring Data Cassandra |
||
Starter for using Couchbase document-oriented database and Spring Data Couchbase |
||
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch |
||
Starter for using GemFire distributed data store and Spring Data GemFire |
||
Starter for using Spring Data JPA with Hibernate |
||
Starter for using Spring Data LDAP |
||
Starter for using MongoDB document-oriented database and Spring Data MongoDB |
||
Starter for using Neo4j graph database and Spring Data Neo4j |
||
Starter for using Redis key-value data store with Spring Data Redis and the Jedis client |
||
Starter for exposing Spring Data repositories over REST using Spring Data REST |
||
Starter for using the Apache Solr search platform with Spring Data Solr |
||
Starter for building MVC web applications using FreeMarker views |
||
Starter for building MVC web applications using Groovy Templates views |
||
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS |
||
Starter for using Spring Integration |
||
Starter for using JDBC with the Tomcat JDBC connection pool |
||
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to |
||
Starter for using jOOQ to access SQL databases. An alternative to |
||
Starter for JTA transactions using Atomikos |
||
Starter for JTA transactions using Bitronix |
||
Spring Boot Narayana JTA Starter |
||
Starter for using Java Mail and Spring Framework’s email sending support |
||
Starter for building web applications using Spring Mobile |
||
Starter for building MVC web applications using Mustache views |
||
Starter for using Spring Security |
||
Starter for using Spring Social Facebook |
||
Stater for using Spring Social LinkedIn |
||
Starter for using Spring Social Twitter |
||
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito |
||
Starter for building MVC web applications using Thymeleaf views |
||
Starter for using Java Bean Validation with Hibernate Validator |
||
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container |
||
Starter for using Spring Web Services |
||
Starter for building WebSocket applications using Spring Framework’s WebSocket support |
表13.2Spring Boot生产环境启动器
名称 |
描述 |
Pom |
spring-boot-starter-actuator |
使用Spring Boot Actuator提供生产准备功能,可帮助您监控和管理应用程序的启动器 |
|
spring-boot-starter-remote-shell |
使用CRaSH远程shell通过SSH监视和管理您的应用程序的启动器。自1.5以来已弃用 |
最后,SpringBoot还包括一些启动器,如果要排除或替换特定的技术,可以使用它们:
名称 |
描述 |
Pom |
spring-boot-starter-undertow |
使用Undertow作为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案 |
|
spring-boot-starter-jetty |
使用Jetty作为嵌入式servlet容器的启动器。 spring-boot-starter-tomcat的替代方案 |
|
spring-boot-starter-logging |
使用Logback进行日志记录的启动器。默认的日志启动器 |
|
spring-boot-starter-tomcat |
使用Tomcat作为嵌入式servlet容器的启动器。 spring-boot-starter-web的默认servlet容器启动器 |
|
spring-boot-starter-log4j2 |
使用Log4j2进行日志记录的启动器。 spring-boot-start-logging的替代方法 |