javascript
告诉你,Spring Boot 真是个牛逼货
轉(zhuǎn)載自? ?告訴你,Spring Boot 真是個牛逼貨
現(xiàn)在 Spring Boot 非?;?#xff0c;各種技術(shù)文章,各種付費教程,多如牛毛,可能還有些不知道 Spring Boot 的,那它到底是什么呢?有什么用?今天給大家詳細(xì)介紹一下。
Spring Boot 的背景
了解 Spring Boot 必須先說說 Spring 框架!
在 Java 后端框架繁榮的今天,Spring 框架無疑是最最火熱,也是必不可少的開源框架,更是穩(wěn)坐 Java 后端框架的龍頭老大。
用過 Spring 框架的都知道 Spring 能流行是因為它的兩把利器:IOC 和 AOP,IOC 可以幫助我們管理對象的依賴關(guān)系,極大減少對象的耦合性,而 AOP 的切面編程功能可以更方面的使用動態(tài)代理來實現(xiàn)各種動態(tài)方法功能(如事務(wù)、緩存、日志等)。
而要集成 Spring 框架,必須要用到 XML 配置文件,或者注解式的 Java 代碼配置。無論是使用 XML 或者代碼配置方式,都需要對相關(guān)組件的配置有足夠的了解,然后再編寫大量冗長的配置代碼。
然后又有多少開發(fā)人員能精通這些配置呢?如果我們只提供一些配置參數(shù)讓框架能自動配置這些組件,那是不是 so easy?
基于簡化 Spring 快速上手為目的,Spring Boot 框架誕生了!
什么是 Spring Boot?
Spring Boot是 Spring 開源組織下的一個子項目,也是 Spring 組件一站式解決方案,主要是為了簡化使用 Spring 框架的難度,簡省繁重的配置。
Spring Boot提供了各種組件的啟動器(starters),開發(fā)者只要能配置好對應(yīng)組件參數(shù),Spring Boot 就會自動配置,讓開發(fā)者能快速搭建依賴于 Spring 組件的 Java 項目。
Spring Boot不但能創(chuàng)建傳統(tǒng)的 war 包應(yīng)用,還能創(chuàng)建獨立的不依賴于任何外部容器(如:tomcat)的獨立應(yīng)用,使用?java -jar?命令就能啟動。同時,Spring Boot也提供了一個命令行工具來執(zhí)行 Spring 的腳本。
Spring Boot 的官方網(wǎng)站:
https://projects.spring.io/spring-boot/
Spring Boot 的源碼:
https://github.com/spring-projects/spring-boot
Spring Boot 的設(shè)計目標(biāo)
如果你不清楚 Spring 是干嘛用的,那再來看看設(shè)計它的目標(biāo)和初衷是什么!
-
為 Spring 應(yīng)用開發(fā)提供一個更快、更容易上手的入門體驗;
-
提供一系列在大型項目中經(jīng)常用到的公共的非功能性特性,如:內(nèi)嵌入服務(wù)器、安全、度量指標(biāo)、健康檢測、外部化配置;
-
零代碼配置生成及零 XML 配置;
Spring Boot 為什么能這么火?
Spring Boot 為什么能這么火?是因為它有以下幾個特色。
1、獨立運行
Spring Boot內(nèi)嵌了各種 Servlet 容器,Tomcat、Jetty等,現(xiàn)在不再需要打成 war 包部署到容器中,Spring Boot 只要打成一個可執(zhí)行的jar包就能獨立運行,所有的依賴包都在一個 jar 包內(nèi)。
2、簡化 Maven 配置
?
?
如上圖所示,現(xiàn)在只要依賴?spring-boot-starter-web?啟動器包,它包含所有 web 開發(fā)所有的依賴,就能擁有 Spring Web 的能力,極大簡少了 maven 對依賴的配置。
使用 Maven 命令:mvn dependency:tree?也可以看到完整的依賴樹:
[INFO]?+-?org.springframework.boot:spring-boot-starter-web:jar:2.0.1.RELEASE:compile [INFO]?|??+-?org.springframework.boot:spring-boot-starter:jar:2.0.1.RELEASE:compile [INFO]?|??|??+-?org.springframework.boot:spring-boot:jar:2.0.1.RELEASE:compile [INFO]?|??|??+-?org.springframework.boot:spring-boot-autoconfigure:jar:2.0.1.RELEASE:compile [INFO]?|??|??+-?org.springframework.boot:spring-boot-starter-logging:jar:2.0.1.RELEASE:compile [INFO]?|??|??|??+-?ch.qos.logback:logback-classic:jar:1.2.3:compile [INFO]?|??|??|??|??\-?ch.qos.logback:logback-core:jar:1.2.3:compile [INFO]?|??|??|??+-?org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile [INFO]?|??|??|??|??\-?org.apache.logging.log4j:log4j-api:jar:2.10.0:compile [INFO]?|??|??|??\-?org.slf4j:jul-to-slf4j:jar:1.7.25:compile [INFO]?|??|??+-?javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO]?|??|??\-?org.yaml:snakeyaml:jar:1.19:runtime [INFO]?|??+-?org.springframework.boot:spring-boot-starter-json:jar:2.0.1.RELEASE:compile [INFO]?|??|??+-?com.fasterxml.jackson.core:jackson-databind:jar:2.9.5:compile [INFO]?|??|??|??+-?com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO]?|??|??|??\-?com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile [INFO]?|??|??+-?com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.5:compile [INFO]?|??|??+-?com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.5:compile [INFO]?|??|??\-?com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.5:compile [INFO]?|??+-?org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.1.RELEASE:compile [INFO]?|??|??+-?org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.29:compile [INFO]?|??|??+-?org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.29:compile [INFO]?|??|??\-?org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.29:compile [INFO]?|??+-?org.hibernate.validator:hibernate-validator:jar:6.0.9.Final:compile [INFO]?|??|??+-?javax.validation:validation-api:jar:2.0.1.Final:compile [INFO]?|??|??+-?org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile [INFO]?|??|??\-?com.fasterxml:classmate:jar:1.3.4:compile [INFO]?|??+-?org.springframework:spring-web:jar:5.0.5.RELEASE:compile [INFO]?|??|??\-?org.springframework:spring-beans:jar:5.0.5.RELEASE:compile [INFO]?|??\-?org.springframework:spring-webmvc:jar:5.0.5.RELEASE:compile [INFO]?|?????+-?org.springframework:spring-aop:jar:5.0.5.RELEASE:compile [INFO]?|?????+-?org.springframework:spring-context:jar:5.0.5.RELEASE:compile [INFO]?|?????\-?org.springframework:spring-expression:jar:5.0.5.RELEASE:compile3、自動配置
Spring Boot能根據(jù)當(dāng)前類路徑下的類或者 jar 包里面來的類來自動配置 Spring Bean,如添加一個?spring-boot-starter-web?啟動器就能擁有 web 的功能,無需其他配置。也可以在配置文件中添加相關(guān)配置來自定義裝配,這個后面的課程會講到,請關(guān)注后續(xù)內(nèi)容。
4、無代碼生成和XML配置
Spring Boot配置過程中無代碼生成,也無需XML配置文件就能完成所有配置工作,這一切都是借助于條件注解完成的,這也是 Spring 4+ 的核心功能之一。
5、應(yīng)用監(jiān)控
Spring Boot提供一系列端點可以監(jiān)控服務(wù)及應(yīng)用,能對Spring 應(yīng)用做健康檢測。
Spring Boot 的缺點
說了這么多 Spring Boot 的優(yōu)點和特色,雖然上手很容易,但也不是沒有缺點。任何框架都有其優(yōu)缺點,Spring Boot 也不例外,我大概可以總結(jié)有以下幾個缺點。
1、需要非常了解 Spring Boot 的核心技術(shù)原理,不然一旦遇到問題就很棘手。因為什么東西都集成了,自動配置化。這樣,需要對配置信息非常熟悉,要找起問題來不是很容易。
2、從原始 Spring 項目很難平滑遷移至 Spring Boot 框架上來,因為有些歷史老舊的 XML 配置無法通過 Java 來配置,還需要額外的 XML 文件就不是很完美。再比如,之前是獨立的 Tomcat,什么參數(shù)都在線上配置好了,你改為內(nèi)置的 Tomcat 就會遇到很多問題。
相對于優(yōu)點來說,這些缺點也都不算什么??傊?#xff0c;Spring Boot 是值得我們?nèi)魏我粋€ Java 開發(fā)者嘗試摸索的。
總結(jié)
以上是生活随笔為你收集整理的告诉你,Spring Boot 真是个牛逼货的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样检查CPU的温度呀如何检测CPU的温
- 下一篇: 紧急整理了 20 道 Spring Bo