一直在构建版本_构建系统与代码结构SpringBoot
從今天開始,我們進(jìn)入SpringBoot的使用環(huán)節(jié),這一部分包含了構(gòu)建系統(tǒng),自動(dòng)配置,如何運(yùn)行應(yīng)用程序,自然也包括了一些使用SpringBoot的最佳實(shí)踐。關(guān)于SpringBoot的介紹,Anders在上一次的分享中已經(jīng)說得很詳細(xì)了,這里我就不多介紹了。SpringBoot使用起來很方便,很快,走位也很拉風(fēng),開箱即用。特別適合這個(gè)追求快騷猛的高效開發(fā)浪潮。
構(gòu)建系統(tǒng)與依賴管理
Spring Boot官方強(qiáng)烈建議我們使用具有依賴管理特性的構(gòu)建系統(tǒng),并且它也建議我們選擇Maven或Gradle。當(dāng)然,你也可以選擇其他諸如Ant之類的構(gòu)建系統(tǒng)來使用Spring Boot,只不過支持的不是特別好。
Maven用戶可以繼承spring-boot-starter-parent來獲取一些合理的默認(rèn)配置。父項(xiàng)目提供了以下的一些配置:
Maven構(gòu)建的兩種方式
如果我們跟著文檔一步步走的話,那么毫不猶豫會(huì)選擇繼承這樣的一種方式來搭建Spring Boot項(xiàng)目,我們只需要在項(xiàng)目的pom中添加這樣一組代碼就可以了:
<!-- Inherit defaults from Spring Boot --> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.RELEASE</version> </parent>在這里,你需要指定SpringBoot的版本,當(dāng)然如果你在依賴中導(dǎo)入了其他的Starter的話,那么你可以不用指定.
如果你需要覆蓋Starter或SpringBoot中的一些類庫的版本時(shí),你只需要在pom的Properties里添加相應(yīng)的屬性來指定版本即可:
<properties><spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>可以檢查 spring-boot-dependencies pom 里的列表,來找到對(duì)應(yīng)模塊的配置屬性名稱
當(dāng)然,并不是每個(gè)人都想要繼承Spring-boot-starter-parent的pom,可能基于以下的原因,你不想直接繼承spring-boot-starter-parent的pom:
此時(shí),如果大家熟悉Spring的BOM,就知道BOM是可以對(duì)Spring版本進(jìn)行控制的,它可以幫助我們統(tǒng)一我們所使用的Spring相關(guān)的版本號(hào).SpringBoot作為Spring出的一款產(chǎn)品,自然也就支持BOM的形式.
如果你不想使用spring-boot-starter-parent, 你就可以通過使用scope=import來讓自己的項(xiàng)目使用Spring Boot.
<dependencyManagement><dependencies><dependency><!-- Import dependency management from Spring Boot --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.0.5.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>我們剛剛提到,使用繼承的方式,允許我們來改變某一個(gè)組件的版本號(hào),并覆蓋默認(rèn)的,當(dāng)時(shí)我們是使用屬性配置的形式,我們剛剛又說bom在版本上給了父級(jí)項(xiàng)目更大的權(quán)限,那么是否是說SpringBoot再使用BOM的時(shí)候就絕對(duì)無法覆蓋默認(rèn)版本呢?當(dāng)然不是的.我們可以使用以下方式來實(shí)現(xiàn)同樣的效果.我們觀察到在這里我們?cè)趕pring-boot-depencies的前方添加了我們想要替換的組件.此時(shí)就可以進(jìn)行覆蓋了.并且在使用BOM的時(shí)候,只可以使用這樣的形式覆蓋.
<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.0.5.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>關(guān)于SpringBoot的可執(zhí)行jar
在之前的項(xiàng)目開發(fā)中,如果我們想要部署一個(gè)web項(xiàng)目,是需要依賴web服務(wù)器的,比如tomcat,或者apache,但SpringBoot則不同,他提供了一個(gè)maven插件,允許我們直接將項(xiàng)目打包成一個(gè)可執(zhí)行的jar文件.大家如果感興趣,可以用命令行看看這兩個(gè)文件的內(nèi)容,或許會(huì)有"驚喜"發(fā)現(xiàn).這里不給你賣關(guān)子了,你會(huì)發(fā)現(xiàn)這兩個(gè)一個(gè)是普通的jar文件,另一個(gè)則是一個(gè)可執(zhí)行的二進(jìn)制文件.spring的maven插件配置如下:
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins> </build>SpringBoot中的Starters
我們剛剛提到使用Spring Boot搭建項(xiàng)目很快,為什么呢?因?yàn)樗蠸tarters.那什么是Starters呢?SpringBoot非常人性化的將常用技術(shù)分類,并把他們提供成一個(gè)公共的模塊,而你只需要引入一個(gè)公共的模塊,這個(gè)模塊所依賴的相關(guān)類庫就會(huì)自動(dòng)的引入到項(xiàng)目中.有點(diǎn)兒類似于我們的項(xiàng)目模版.,這是SpringBoot非常方便的一個(gè)地方.也因此,用SpringBoot搭建一個(gè)項(xiàng)目,要比用傳統(tǒng)方式快很多。
關(guān)于Starters,有幾點(diǎn)要說的,首先就是名字.SpringBoot不但自己提供了Starters,它也允許我們定義自己的Starters.官方定義的一般都是spring-boot-starter-*,而如果是我們自定義的或者是第三方的名字則為:*-spring-boot-starter
這里最明顯也是最有參考價(jià)值的,應(yīng)該就是mybatis了.mybatis一直都不怎么受Spring待見,所以別的(如hibernate)都是Spring出適配,而到了Mybatis這里,幾乎都是Mybatis出適配,所以我們?cè)谑褂玫臅r(shí)候看到的mybatis-spring而不是spring-mybatis.這里也是,在SpringBoot提供的眾多組件中,仍然沒有Mybatis的,但Mybatis則出了一個(gè)可以適配Spring-boot的starters:mybatis-spring-boot .
SpringBoot官方定義的Starters.這些你可以不用死記硬背
NameDescriptionPom
spring-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實(shí)踐中的最佳代碼結(jié)構(gòu)
每一種技術(shù)都有其對(duì)應(yīng)的組織代碼的方式,比如我們?cè)谖覀兯煜さ膍vc里,我們通常會(huì)將實(shí)體類放入到Model包里,控制層,我們則放入到Controller里,然后視圖層則相應(yīng)的放入到View里.業(yè)務(wù)層放到Service層中,而數(shù)據(jù)庫操作一般放入到dao層或者mapper里.為什么這么放呢?其實(shí)并沒有什么強(qiáng)制性的要求,而是大家達(dá)成的一種約定.最后,這樣的約定大于配置,就成了項(xiàng)目組織方式的一種最佳實(shí)踐.說起約定大于配置,Maven就是最佳的例子.
雖然SpringBoot沒有強(qiáng)制使用某一種代碼布局,但他也推薦了關(guān)于這方面的最佳實(shí)踐.
這個(gè)是代碼的目錄組織方式:
這個(gè)是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,很多人喜歡他主要是因?yàn)樗目炫c它的高效.這也是Spring家族一貫的特色,這也是它的成功之處.它不但將自己的一些模塊像MVC,JPA,Security等整合了起來,也整合了一些優(yōu)秀的第三庫,并用starter這樣的形式進(jìn)行組織,使得我們可以很方便的構(gòu)建出適合自己應(yīng)用場景的項(xiàng)目骨架,這是非常迷人的.并且很少有架構(gòu)能夠像SpringBoot一樣,把靈活與簡單做的如此極致.
相信隨著深入的學(xué)習(xí),你會(huì)越來越喜歡SpringBoot
2019年最新java入門到架構(gòu),資源整合大禮包關(guān)注微信公眾號(hào)“熵增學(xué)院”即可領(lǐng)取~
一線架構(gòu)師整理全套學(xué)習(xí)視頻和資料,都在這里等你!
總結(jié)
以上是生活随笔為你收集整理的一直在构建版本_构建系统与代码结构SpringBoot的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用element-ui进行文件的上传
- 下一篇: reg类型变量综合电路_2014年PLD