從今天開始,我們進入SpringBoot的使用環節,這一部分包含了構建系統,自動配置,如何運行應用程序,自然也包括了一些使用SpringBoot的最佳實踐.關於SpringBoot的介紹,Anders在上一次的分享中已經說得很詳細了,這裡我就不多介紹了.SpringBoot使用起來很方便,很快,走位也很拉風,開箱即用.特別適合這個追求快騷猛的高效開發浪潮.
構建系統與依賴管理
Spring Boot官方強烈建議我們使用具有依賴管理特性的構建系統,並且它也建議我們選擇Maven或Gradle。當然,你也可以選擇其他諸如Ant之類的構建系統來使用Spring Boot,只不過支持的不是特別好。
- 每一個Spring Boot的穩定版都會提供一組它所支持的依賴清單。在開發的時候,你沒必要在你的構建配置中提供那些依賴的版本,因為Spring Boot會為你管理依賴的版本信息。而當你更新Spring Boot版本的時候,這些依賴的版本也會以一種很方便的形式更新。當然,如果有需求的話,你也可以指定然後覆蓋Spring Boot建議的版本。
- Spring Boot 的策略清單裡不但包含了你在使用Spring Boot時會用到的所有Spring的模塊,也包含了一些比較優秀的第三方插件庫。這一切的素材對Maven和Gradle來說,都是可用的。每一個穩定的Spring Boot版本都會關聯一個特定的Spring版本,高度建議不要改變Spring的版本。
Maven用戶可以繼承spring-boot-starter-parent來獲取一些合理的默認配置。父項目提供了以下的一些配置:
- JDK8作為默認編譯
- UTF8作為編碼字符集
- 父級項目包含了一系列被普遍使用的類庫的版本,它幫你管理了這些依賴的版本,你再也不用在你的pom中配置version了
- 包含了一些比較智能的資源過濾配置
- 還有一些插件配置
- 也允許你在配置文件(application.properties,application.yml)裡配置你的過濾規則它的配置文件也支持Spring風格的變量佔位符${},而Maven的佔位符則變為了@..@,你也可以用resource.delimiter來覆蓋
Maven構建的兩種方式
如果我們跟著文檔一步步走的話,那麼毫不猶豫會選擇繼承這樣的一種方式來搭建Spring Boot項目,我們只需要在項目的pom中添加這樣一組代碼就可以了:
org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE
在這裡,你需要指定SpringBoot的版本,當然如果你在依賴中導入了其他的Starter的話,那麼你可以不用指定.
如果你需要覆蓋Starter或SpringBoot中的一些類庫的版本時,你只需要在pom的Properties裡添加相應的屬性來指定版本即可:
Fowler-SR2
可以檢查 spring-boot-dependencies pom 裡的列表,來找到對應模塊的配置屬性名稱
當然,並不是每個人都想要繼承Spring-boot-starter-parent的pom,可能基於以下的原因,你不想直接繼承spring-boot-starter-parent的pom:
- 你有你自己的團隊協作標準,類庫依賴標準等等
- 你想要自己精確的掌控項目中的Maven配置,不想讓人隨意更改項目中類庫的版本號
此時,如果大家熟悉Spring的BOM,就知道BOM是可以對Spring版本進行控制的,它可以幫助我們統一我們所使用的Spring相關的版本號.SpringBoot作為Spring出的一款產品,自然也就支持BOM的形式.
如果你不想使用spring-boot-starter-parent, 你就可以通過使用scope=import來讓自己的項目使用Spring Boot.
org.springframework.boot spring-boot-dependencies 2.0.5.RELEASE pom import
我們剛剛提到,使用繼承的方式,允許我們來改變某一個組件的版本號,並覆蓋默認的,當時我們是使用屬性配置的形式,我們剛剛又說bom在版本上給了父級項目更大的權限,那麼是否是說SpringBoot再使用BOM的時候就絕對無法覆蓋默認版本呢?當然不是的.我們可以使用以下方式來實現同樣的效果.我們觀察到在這裡我們在spring-boot-depencies的前方添加了我們想要替換的組件.此時就可以進行覆蓋了.並且在使用BOM的時候,只可以使用這樣的形式覆蓋.
org.springframework.data spring-data-releasetrain Fowler-SR2 pom import org.springframework.boot spring-boot-dependencies 2.0.5.RELEASE pom import
關於SpringBoot的可執行jar
在之前的項目開發中,如果我們想要部署一個web項目,是需要依賴web服務器的,比如tomcat,或者apache,但SpringBoot則不同,他提供了一個maven插件,允許我們直接將項目打包成一個可執行的jar文件.大家如果感興趣,可以用命令行看看這兩個文件的內容,或許會有"驚喜"發現.這裡不給你賣關子了,你會發現這兩個一個是普通的jar文件,另一個則是一個可執行的二進制文件.spring的maven插件配置如下:
org.springframework.boot spring-boot-maven-plugin
SpringBoot中的Starters
我們剛剛提到使用Spring Boot搭建項目很快,為什麼呢?因為它有Starters.那什麼是Starters呢?SpringBoot非常人性化的將常用技術分類,並把他們提供成一個公共的模塊,而你只需要引入一個公共的模塊,這個模塊所依賴的相關類庫就會自動的引入到項目中.有點兒類似於我們的項目模版.,這是SpringBoot非常方便的一個地方.也因此,用SpringBoot搭建一個項目,要比用傳統方式快很多。
關於Starters,有幾點要說的,首先就是名字.SpringBoot不但自己提供了Starters,它也允許我們定義自己的Starters.官方定義的一般都是spring-boot-starter-*,而如果是我們自定義的或者是第三方的名字則為:*-spring-boot-starter
這裡最明顯也是最有參考價值的,應該就是mybatis了.mybatis一直都不怎麼受Spring待見,所以別的(如hibernate)都是Spring出適配,而到了Mybatis這裡,幾乎都是Mybatis出適配,所以我們在使用的時候看到的mybatis-spring而不是spring-mybatis.這裡也是,在SpringBoot提供的眾多組件中,仍然沒有Mybatis的,但Mybatis則出了一個可以適配Spring-boot的starters:mybatis-spring-boot .
SpringBoot官方定義的Starters.這些你可以不用死記硬背
NameDescriptionPomspring-boot-starter
Core starter, including auto-configuration support, logging and YAML
Pom
spring-boot-starter-activemq
Starter for JMS messaging using Apache ActiveMQ
Pom
spring-boot-starter-amqp
Starter for using Spring AMQP and Rabbit MQ
Pom
spring-boot-starter-aop
Starter for aspect-oriented programming with Spring AOP and AspectJ
Pom
spring-boot-starter-artemis
Starter for JMS messaging using Apache Artemis
Pom
spring-boot-starter-batch
Starter for using Spring Batch
Pom
spring-boot-starter-cache
Starter for using Spring Framework’s caching support
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-data-cassandra
Starter for using Cassandra distributed database and Spring Data Cassandra
Pom
spring-boot-starter-data-cassandra-reactive
Starter for using Cassandra distributed database and Spring Data Cassandra Reactive
Pom
spring-boot-starter-data-couchbase
Starter for using Couchbase document-oriented database and Spring Data Couchbase
Pom
spring-boot-starter-data-couchbase-reactive
Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive
Pom
spring-boot-starter-data-elasticsearch
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch
Pom
spring-boot-starter-data-jpa
Starter for using Spring Data JPA with Hibernate
Pom
spring-boot-starter-data-ldap
Starter for using Spring Data LDAP
Pom
spring-boot-starter-data-mongodb
Starter for using MongoDB document-oriented database and Spring Data MongoDB
Pom
spring-boot-starter-data-mongodb-reactive
Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive
Pom
spring-boot-starter-data-neo4j
Starter for using Neo4j graph database and Spring Data Neo4j
Pom
spring-boot-starter-data-redis
Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client
Pom
spring-boot-starter-data-redis-reactive
Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client
Pom
spring-boot-starter-data-rest
Starter for exposing Spring Data repositories over REST using Spring Data REST
Pom
spring-boot-starter-data-solr
Starter for using the Apache Solr search platform with Spring Data Solr
Pom
spring-boot-starter-freemarker
Starter for building MVC web applications using FreeMarker views
Pom
spring-boot-starter-groovy-templates
Starter for building MVC web applications using Groovy Templates views
Pom
spring-boot-starter-hateoas
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS
Pom
spring-boot-starter-integration
Starter for using Spring Integration
Pom
spring-boot-starter-jdbc
Starter for using JDBC with the HikariCP connection pool
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-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-json
Starter for reading and writing json
Pom
spring-boot-starter-jta-atomikos
Starter for JTA transactions using Atomikos
Pom
spring-boot-starter-jta-bitronix
Starter for JTA transactions using Bitronix
Pom
spring-boot-starter-jta-narayana
Starter for JTA transactions using Narayana
Pom
spring-boot-starter-mail
Starter for using Java Mail and Spring Framework’s email sending support
Pom
spring-boot-starter-mustache
Starter for building web applications using Mustache views
Pom
spring-boot-starter-quartz
Starter for using the Quartz scheduler
Pom
spring-boot-starter-security
Starter for using Spring Security
Pom
spring-boot-starter-test
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito
Pom
spring-boot-starter-thymeleaf
Starter for building MVC web applications using Thymeleaf views
Pom
spring-boot-starter-validation
Starter for using Java Bean Validation with Hibernate Validator
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-web-services
Starter for using Spring Web Services
Pom
spring-boot-starter-webflux
Starter for building WebFlux applications using Spring Framework’s Reactive Web support
Pom
spring-boot-starter-websocket
Starter for building WebSocket applications using Spring Framework’s WebSocket support
Pom
SpringBoot實踐中的最佳代碼結構
每一種技術都有其對應的組織代碼的方式,比如我們在我們所熟悉的mvc裡,我們通常會將實體類放入到Model包裡,控制層,我們則放入到Controller裡,然後視圖層則相應的放入到View裡.業務層放到Service層中,而數據庫操作一般放入到dao層或者mapper裡.為什麼這麼放呢?其實並沒有什麼強制性的要求,而是大家達成的一種約定.最後,這樣的約定大於配置,就成了項目組織方式的一種最佳實踐.說起約定大於配置,Maven就是最佳的例子.
雖然SpringBoot沒有強制使用某一種代碼佈局,但他也推薦了關於這方面的最佳實踐.
- 首先,關於包,默認包這種方式應該被避免.因為這個會導致在使用SpringBoot的@ComponentScan, @EntityScan, 或 @SpringBootApplication註解的時候出現一些比較奇葩的問題.關於包,SpringBoot建議我們使用Java裡面的那種方式,域名的倒寫.
- 其次,SpringBoot建議我們將SpringBoot的加載器放到我們的根目錄下,與model和controller同級,然後 @SpringBootApplication這個註解是要放在SpringBoot加載器的class上,@SpringBootApplication可以簡單的被理解為是@EnableAutoConfiguration 和 @ComponentScan的合集
- 這個是代碼的目錄組織方式:
com +- example +- myapplication +- Application.java | +- customer | +- Customer.java | +- CustomerController.java | +- CustomerService.java | +- CustomerRepository.java | +- order +- Order.java +- OrderController.java +- OrderService.java +- OrderRepository.java
這個是SpringBoot的加載器.
package com.example.myapplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
後記
SpringBoot,很多人喜歡他主要是因為它的快與它的高效.這也是Spring家族一貫的特色,這也是它的成功之處.它不但將自己的一些模塊像MVC,JPA,Security等整合了起來,也整合了一些優秀的第三庫,並用starter這樣的形式進行組織,使得我們可以很方便的構建出適合自己應用場景的項目骨架,這是非常迷人的.並且很少有架構能夠像SpringBoot一樣,把靈活與簡單做的如此極致.
相信隨著深入的學習,你會越來越喜歡SpringBoot