文章作者:Tyan
博客:noahsnail.com | CSDN | 簡書
Part III. 使用Spring Boot
這一節將會講述關于應該如何使用Spring Boot的更多細節。它包括許多主題例如構建系統,自動配置和怎么運行自己的應用。我們也講述一些Spring Boot的最佳實踐。雖然沒有關于Spring Boot非常特別的東西(它只是另一個你可以使用的庫),但接下來的一些建議可以讓你的開發過程更容易一點。
如果你剛開始學習Spring Boot,在學習這節之前你可能應該先閱讀一下『Getting Started』部分。
13. 構建系統
強烈建議你選擇一個支持依賴管理的構建系統,構建系統可以使用發布在『Maven Central』倉庫中的工件。我們建議你選擇Maven或Gradle。Spring Boot可能也可以與其它的構建系統進行協作(例如Ant),但不能特別好的支持其它的構建系統。
13.1 依賴管理
Spring Boot的每一次發布都會提供它支持的依賴列表。實際應用時,在你的構建配置中不需要提供這些依賴的版本,因為Spring Boot會幫你進行管理。當你升級Spring Boot時,這些依賴也會隨之進行升級。
如果有必要的話,你仍可以指定版本并覆蓋Spring Boot的推薦。
這個列表包含了所有你在Spring Boot中可以使用的Spring模塊,也包含了第三方庫的精制列表。這個列表可以當做一個標準可用的Bills of Materials (spring-boot-dependencies),也額外的專門支持Maven和Gradle可用。
Spring Boot的每一次發布都是與Spring框架的基本版本相關的,因此我們強烈建議你在自己使用時不要指定它的版本。
13.2 Maven
Maven用戶可以繼承spring-boot-starter-parent
工程來獲得合理的默認配置。父工程提供了下面的特性:
Java 1.6作為默認的編譯級別。
UTF-8源碼編碼。
依賴管理部分,對于常用的依賴允許你忽略
<version>
標簽,從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
來覆蓋)。
13.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.0.0.BUILD-SNAPSHOT</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支持的屬性清單。
13.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.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
這個設置不允許你使用上面闡述的屬性來重寫單獨的依賴。為了取得同樣的效果,你需要在spring-boot-dependencies
入口之前在工程的dependencyManagement
中的添加一個入口。為了升級另一個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.0.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在上面的例子中,我們指定了一個POM但任何依賴類型都可以被重寫。
13.2.3 更改Java版本
spring-boot-starter-parent
選擇了相當保守的Java兼容性。如果你想聽從我們的建議,使用更新的Java版本,你可以添加java.version
屬性。
<properties>
<java.version>1.8</java.version>
</properties>
13.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 parent pom,你只需要添加這個插件,不需要配置它,除非你想更改父POM中的定義的設置。
13.3 Gradle
Gradle用戶可以直接在它們的dependencies
部分導入starters
。不像Maven,這兒不能導入“super parent”來共享一些配置。
apply plugin: 'java'
repositories {
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:2.0.0.BUILD-SNAPSHOT")
}
spring-boot-gradle-plugin
也可用,并提供了從源碼創建可執行jars和運行項目的功能。它也提供了依賴管理,在其它的兼容性之間,允許你忽略任何Spring Boot管理的依賴的版本號:
buildscript {
repositories {
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.BUILD-SNAPSHOT")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
repositories {
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
13.4 Ant
使用Apache Ant+Ivy來創建一個Spring Boot項目是可能的。spring-boot-antlib
“AntLib”模塊也可以用來幫助Ant創建可執行jars。
為了聲明依賴,一個典型的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="1.3.0.BUILD-SNAPSHOT" />
<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
模塊,請看79.10小節,“Build an executable archive from Ant without using spring-boot-antlib” “How-to”。
13.5 Starters
啟動器是一系列你可以包含進自己應用中的實用依賴描述符。你可以得到所有Spring和你需要的相關技術的一站式服務,不需要有搜索樣例代碼和拷貝粘貼依賴描述符的負擔。例如,如果你想開始使用Spring和JPA來進行數據庫鏈接,只需要在你的工程中包含spring-boot-starter-data-jpa
依賴,你便可以很好的前行了。
啟動器包含許多你需要啟動并快速運行一個工程的依賴,并持續支持一系列傳遞管理的依賴。
What’s in a name
所有的官方啟動器都有一個類似的命名模式:spring-boot-starter-*
,*
是應用的特性類型。這個命名結構用來在你需要時幫助你發現一個啟動器。Maven在許多IDEs中進行了集成,允許你通過名字來搜索依賴。例如,安裝了合適的Eclipse或STS插件,你可以簡單的在POM編輯器中點擊ctrl-space
并輸入“spring-boot-starter”來查找一個完整的列表。
正如在創建你自己的啟動器部分講述的那樣,第三方啟動器不應該與spring-boot
一起啟動,因為它是預留給官方Spring Boot構建的。acme
的第三方啟動器通過命名為acme-spring-boot-starter
。
下面的應用啟動器由Spring Boot提供,在org.springframework.boot
組下:
表13.1. Spring Boot應用啟動器
Name | Description | POM |
---|---|---|
spring-boot-starter-thymeleaf | Starter for building MVC web applications using Thymeleaf views | POM |
spring-boot-starter-data-couchbase | Starter for using Couchbase document-oriented database and Spring Data Couchbase | POM |
spring-boot-starter-artemis | Starter for JMS messaging using Apache Artemis | POM |
spring-boot-starter-web-services | Starter for using Spring Web Services | POM |
spring-boot-starter-mail | Starter for using Java Mail and Spring Framework’s email sending support | POM |
spring-boot-starter-data-redis | Starter for using Redis key-value data store with Spring Data Redis and the Jedis client | POM |
spring-boot-starter-web | Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container | POM |
spring-boot-starter-data-gemfire | Starter for using GemFire distributed data store and Spring Data GemFire | POM |
spring-boot-starter-activemq | Starter for JMS messaging using Apache ActiveMQ | POM |
spring-boot-starter-data-elasticsearch | Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch | POM |
spring-boot-starter-integration | Starter for using Spring Integration | POM |
spring-boot-starter-test | Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito | POM |
spring-boot-starter-jdbc | Starter for using JDBC with the Tomcat JDBC connection pool | POM |
spring-boot-starter-mobile | Starter for building web applications using Spring Mobile | POM |
spring-boot-starter-validation | Starter for using Java Bean Validation with Hibernate Validator | POM |
spring-boot-starter-hateoas | Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS | POM |
spring-boot-starter-jersey | Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web | POM |
spring-boot-starter-data-neo4j | Starter for using Neo4j graph database and Spring Data Neo4j | POM |
spring-boot-starter-websocket | Starter for building WebSocket applications using Spring Framework’s WebSocket support | POM |
spring-boot-starter-aop | Starter for aspect-oriented programming with Spring AOP and AspectJ | POM |
spring-boot-starter-amqp | Starter for using Spring AMQP and Rabbit MQ | POM |
spring-boot-starter-data-cassandra | Starter for using Cassandra distributed database and Spring Data Cassandra | POM |
spring-boot-starter-social-facebook | Starter for using Spring Social Facebook | POM |
spring-boot-starter-jta-atomikos | Starter for JTA transactions using Atomikos | POM |
spring-boot-starter-security | Starter for using Spring Security | POM |
spring-boot-starter-mustache | Starter for building MVC web applications using Mustache views | POM |
spring-boot-starter-data-jpa | Starter for using Spring Data JPA with Hibernate | POM |
spring-boot-starter | Core starter, including auto-configuration support, logging and YAML | POM |
spring-boot-starter-groovy-templates | Starter for building MVC web applications using Groovy Templates views | POM |
spring-boot-starter-freemarker | Starter for building MVC web applications using FreeMarker views | POM |
spring-boot-starter-batch | Starter for using Spring Batch | POM |
spring-boot-starter-social-linkedin | Stater for using Spring Social LinkedIn | POM |
spring-boot-starter-cache | Starter for using Spring Framework’s caching support | POM |
spring-boot-starter-data-solr | Starter for using the Apache Solr search platform with Spring Data Solr | POM |
spring-boot-starter-data-mongodb | Starter for using MongoDB document-oriented database and Spring Data MongoDB | POM |
spring-boot-starter-jooq | Starter for using jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc | POM |
spring-boot-starter-jta-narayana | Spring Boot Narayana JTA Starter | POM |
spring-boot-starter-cloud-connectors | Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku | POM |
spring-boot-starter-jta-bitronix | Starter for JTA transactions using Bitronix | POM |
spring-boot-starter-social-twitter | Starter for using Spring Social Twitter | POM |
spring-boot-starter-data-rest | Starter for exposing Spring Data repositories over REST using Spring Data REST | POM |
除了應用啟動器之外,下面的啟動器可以用來添加產品準備功能:
Table 13.2. Spring Boot 產品啟動器
Name | Description | Pom |
---|---|---|
spring-boot-starter-actuator | Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application | Pom |
最后,如果你想排除或交換特定的技術方面,Spring Boot也包括一些可以使用的啟動器:
Table 13.3. Spring Boot 技術啟動器
Name | Description | Pom |
---|---|---|
spring-boot-starter-undertow | Starter for using Undertow as the embedded servlet container. An alternative to spring-boot-starter-tomcat | Pom |
spring-boot-starter-jetty | Starter for using Jetty as the embedded servlet container. An alternative to spring-boot-starter-tomcat | Pom |
spring-boot-starter-logging | Starter for logging using Logback. Default logging starter | Pom |
spring-boot-starter-tomcat | Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web
|
Pom |
spring-boot-starter-log4j2 | Starter for using Log4j2 for logging. An alternative to spring-boot-starter-logging
|
Pom |
對于額外的社區共享的啟動器,請看GitHub上
the spring-boot-starters
模塊的README file。