摘要:現(xiàn)在這還是一個空的項(xiàng)目,我們可以在標(biāo)簽中添加我們需要的依賴,例如添加的依賴。修改我們的配置如下目前我們的這個項(xiàng)目還沒有導(dǎo)入任何,這點(diǎn)可以通過執(zhí)行命令確定。
本篇文章是SpringBoot最入門的介紹。我們不借助任何額外的工具,從無到有創(chuàng)建一個Spring Boot的web項(xiàng)目,并運(yùn)行這個項(xiàng)目。
項(xiàng)目構(gòu)建歸根結(jié)底,Spring Boot就只是一個框架,幾個jar而已,沒什么神奇的。但使用Spring Initializr創(chuàng)建項(xiàng)目的過程把很多信息屏蔽掉了,這樣我們就很難搞清楚Spring Boot的本質(zhì)是什么。下面僅使用maven從無到有構(gòu)建一個Spring Boot的web項(xiàng)目。
先創(chuàng)建一個maven空工程如下所示,項(xiàng)目的名字叫spring-boot-hello。
4.0.0 com.poype spring-boot-hello 1.0-SNAPSHOT
現(xiàn)在這還是一個空的maven項(xiàng)目,我們可以在dependencies標(biāo)簽中添加我們需要的依賴,例如添加Spring Boot的依賴。但是Spring Boot為了減少配置,方便我們開發(fā),提供了一個parent maven工程spring-boot-starter-parent,我們只要讓我們的這個項(xiàng)目繼承spring-boot-starter-parent工程,就能減少好多配置。修改我們的POM配置如下:
4.0.0 com.poype spring-boot-hello 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE
目前我們的這個maven項(xiàng)目還沒有導(dǎo)入任何dependency,這點(diǎn)可以通過執(zhí)行mvn dependency:tree命令確定。
我們要創(chuàng)建的是一個web項(xiàng)目,所以添加spring-boot-starter-web這個依賴。修改POM配置如下:
4.0.0 com.poype spring-boot-hello 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE org.springframework.boot spring-boot-starter-web
由于在spring-boot-starter-parent的dependencyManagement中已經(jīng)用聲明了spring-boot-starter-web,所以此處我們可以省略它的version配置。
再次執(zhí)行mvn dependency:tree命令獲得如下結(jié)果:
[INFO] Scanning for projects... [INFO] [INFO] --------------------< com.poype:spring-boot-hello >--------------------- [INFO] Building spring-boot-hello 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ spring-boot-hello --- [INFO] com.poype:spring-boot-hello:jar:1.0-SNAPSHOT [INFO] - org.springframework.boot:spring-boot-starter-web:jar:2.1.4.RELEASE:compile [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.4.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.4.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.slf4j:slf4j-api:jar:1.7.26:compile [INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile [INFO] | | | - org.apache.logging.log4j:log4j-api:jar:2.11.2:compile [INFO] | | - org.slf4j:jul-to-slf4j:jar:1.7.26:compile [INFO] | +- javax.annotation:javax.annotation-api:jar:1.3.2:compile [INFO] | +- org.springframework:spring-core:jar:5.1.6.RELEASE:compile [INFO] | | - org.springframework:spring-jcl:jar:5.1.6.RELEASE:compile [INFO] | - org.yaml:snakeyaml:jar:1.23:runtime [INFO] +- org.springframework.boot:spring-boot-starter-json:jar:2.1.4.RELEASE:compile [INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile [INFO] | | - com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.8:compile [INFO] | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.8:compile [INFO] | - com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.8:compile [INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.4.RELEASE:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.17:compile [INFO] | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.17:compile [INFO] | - org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.17:compile [INFO] +- org.hibernate.validator:hibernate-validator:jar:6.0.16.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.4.0:compile [INFO] +- org.springframework:spring-web:jar:5.1.6.RELEASE:compile [INFO] | - org.springframework:spring-beans:jar:5.1.6.RELEASE:compile [INFO] - org.springframework:spring-webmvc:jar:5.1.6.RELEASE:compile [INFO] +- org.springframework:spring-aop:jar:5.1.6.RELEASE:compile [INFO] +- org.springframework:spring-context:jar:5.1.6.RELEASE:compile [INFO] - org.springframework:spring-expression:jar:5.1.6.RELEASE:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.018 s [INFO] Finished at: 2019-05-19T22:54:50+08:00 [INFO] ------------------------------------------------------------------------
可以看到在添加spring-boot-starter-web這個依賴后,有許多的jar都被導(dǎo)入了。
除了更多的spring-boot-starter-*被導(dǎo)入了之外,更重要的是很多Spring Framework的jar也被導(dǎo)入了,包括spring-core、spring-beans、spring-context、spring-aop等等。另外還有與tomcat相關(guān)的jar也被導(dǎo)入了,也就是說現(xiàn)在我們已經(jīng)有了可以運(yùn)行web程序的servlet容器了。
工程配置已經(jīng)完成,新建一個HelloController測試類,輸入如下代碼:
package com.poype.springboot.web; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class HelloController { @RequestMapping("/hello") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(HelloController.class, args); } }
現(xiàn)在,我們已經(jīng)完成了一個簡單的web應(yīng)用開發(fā),可以啟動我們這個應(yīng)用了。
由于我們的工程繼承了spring-boot-starter-parent的POM配置,它提供了啟動spring-boot應(yīng)用的相關(guān)插件(該插件的run目標(biāo)用于啟動應(yīng)用),可以通過執(zhí)行mvn spring-boot:run命令啟動應(yīng)用,得到如下運(yùn)行結(jié)果。
從運(yùn)行結(jié)果中可以看到,spring-boot啟動了Tomcat服務(wù)器,并監(jiān)聽在8080端口。下面我們打開瀏覽器,輸入地址http://localhost:8080/hello就可以看到程序運(yùn)行結(jié)果。
應(yīng)用打包應(yīng)用構(gòu)建好之后,需要build出一個應(yīng)用包才能用于生產(chǎn)部署。為此需要在POM配置中新增一個插件,修改POM配置如下:
4.0.0 com.poype spring-boot-hello 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.4.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-maven-plugin
接著我們執(zhí)行mvn clean package命令,可以在target目錄下發(fā)現(xiàn)構(gòu)建好的應(yīng)用包 spring-boot-hello-1.0-SNAPSHOT.jar。
執(zhí)行java -jar ./target/spring-boot-hello-1.0-SNAPSHOT.jar命令就可以啟動應(yīng)用了。
上面的例子雖然非常簡單,但卻也是一個標(biāo)準(zhǔn)的spring web應(yīng)用。我們可以回憶一下,如果沒有Spring Boot,創(chuàng)建一個這樣的web應(yīng)用都需要哪些步驟呢?首先要在maven的POM中導(dǎo)入N多相關(guān)dependency(包括Spring的、servlet的、json的...)。然后添加各種復(fù)雜配置(包括servlet的、Spring的、Spring MVC的...)。最后寫完代碼build好一個war包,我們還需要下載一個Tomcat,并將war包放到tomcat下的指定路徑,啟動tomcat部署應(yīng)用。這個過程即使是工作幾年的老司機(jī),從無到有創(chuàng)建一個項(xiàng)目估計也要十幾分鐘,如果是新手再遇到一些問題,解決起來就更麻煩了,可能幾個小時也不一定能搞得出來。
使用Spring Boot構(gòu)建應(yīng)用,即便我們僅僅使用maven,也幾乎沒有什么配置。如果使用Spring Initializr的話,創(chuàng)建好工程無需任何配置就直接可以寫代碼了,非常的方便,即使是新手幾分鐘也能搞出來這個HelloWorld應(yīng)用。這就是Spring Boot給我的最初印象,但是它是如何做到這些的呢?
約定大于配置
Spring Boot提供了很多Starter依賴,每種類型的Starter提供了這種類型應(yīng)用可能需要的一系列dependency(利用maven間接依賴的特性)。例如我們這里創(chuàng)建的是一個web應(yīng)用,所以我們的項(xiàng)目依賴spring-boot-starter-web,而spring-boot-starter-web會將web開發(fā)可能需要的依賴全部幫我們導(dǎo)入,省去很多配置的工作。spring-boot-starter-parent是一個特殊的starter,它提供了許多maven默認(rèn)配置,如dependenceManagment。
另一個比較重要的是注解@EnableAutoConfiguration,Spring Boot看到這個注解,會根據(jù)已經(jīng)加入的jar dependency執(zhí)行相關(guān)的配置。例如在我們的工程中有Spring MVC和Tomcat的依賴,Spring Boot就會猜到這是一個WEB工程,它就會對項(xiàng)目執(zhí)行相應(yīng)的配置(如Spring MVC和Servlet的配置)。
應(yīng)用啟動
Spring Boot自帶了一個Tomcat容器,省去我們自己安裝和配置容器的工作。為了了解Spring Boot的啟動過程,我們將build好的jar解壓得到的目錄結(jié)構(gòu)如下圖所示:
其中最主要的配置文件是MANIFEST.MF,在執(zhí)行java -jar *.jar啟動命令時,JVM參考的就是這個文件的配置。其文件內(nèi)容如下:
Manifest-Version: 1.0 Implementation-Title: spring-boot-hello Implementation-Version: 1.0-SNAPSHOT Built-By: poype Implementation-Vendor-Id: com.poype Spring-Boot-Version: 2.1.4.RELEASE Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: com.poype.springboot.web.HelloController Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Created-By: Apache Maven 3.6.1 Build-Jdk: 1.8.0_211 Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo ot-starter-parent/spring-boot-hello
Main-Class是jar包中的啟動類,可以看到是一個叫org.springframework.boot.loader.JarLauncher類,是Spring Boot提供的Launcher類。Start-Class是我們自己編寫的含有main方法的類。JarLauncher的代碼如下:
public class JarLauncher extends ExecutableArchiveLauncher { static final String BOOT_INF_CLASSES = "BOOT-INF/classes/"; static final String BOOT_INF_LIB = "BOOT-INF/lib/"; public JarLauncher() {} protected JarLauncher(Archive archive) { super(archive); } @Override protected boolean isNestedArchive(Archive.Entry entry) { if (entry.isDirectory()) { return entry.getName().equals(BOOT_INF_CLASSES); } return entry.getName().startsWith(BOOT_INF_LIB); } public static void main(String[] args) throws Exception { new JarLauncher().launch(args); } }
從代碼中可以看出,BOOT-INF/classes/路徑下存放了應(yīng)用程序自己的類,BOOT-INF/lib/路徑下存放了第三方依賴的jar,包括內(nèi)嵌的tomcat jar。
由于這個Jar中既包含應(yīng)用程序自己的類,又包含應(yīng)用所依賴的第三方的Jar,還包含Spring Boot Loader相關(guān)的類,所以這個Jar被稱作Fat Jar。
在JarLauncher類的main函數(shù)中,通過launch方法啟動整個應(yīng)用。launch方法的實(shí)現(xiàn)在父類Launcher中。在Launcher方法中,會根據(jù)MANIFEST.MF中Start-Class的配置項(xiàng)找到我們自己的Main Class,然后構(gòu)造一個ClassLoader加載應(yīng)用類和第三方的Jar,最后會創(chuàng)建一個新的線程執(zhí)行應(yīng)用程序自己的main函數(shù)。
看到這里我們可以大概總結(jié)一下,Spring Boot實(shí)現(xiàn)了一套自己的部署路徑規(guī)范(應(yīng)用自己的類放在哪里,應(yīng)用依賴的第三方j(luò)ar放在哪里等等),就像J2EE規(guī)范一樣。然后利用tomcat的jar實(shí)現(xiàn)servlet容器的功能,對WEB請求進(jìn)行處理。
可以說Spring Boot利用tomcat打造了一個全新的平臺,這個平臺也僅僅只在servlet容器部分利用到了tomcat的功能,至于部署規(guī)范和加載機(jī)制,都是Spring Boot自己全新實(shí)現(xiàn)的。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/74541.html
摘要:默認(rèn)使用了內(nèi)嵌容器支持開箱即用。備注關(guān)于第三步注解的一些描述該注解是引入的。查看源碼可知其包含了和注解。我們可以將其看做對注解的增強(qiáng)與細(xì)分常用來返回格式的數(shù)據(jù)。 導(dǎo)讀: 通過上篇文章, 我們已經(jīng)了解到了 Spring Boot 作為一個Spring的腳手架, 其核心思想便是約定大于配置,通過一層層的封裝讓我們可以在最短的時間內(nèi)搭建一個web項(xiàng)目,從繁瑣的配置中走出來更加關(guān)注業(yè)務(wù)代碼。...
摘要:初衷看了一下相關(guān)的書籍,創(chuàng)建一個的應(yīng)用,是那么的簡單。首先,我們只是創(chuàng)建一個簡單的并不打算使用默認(rèn)的,而是使用傳統(tǒng)的。在下創(chuàng)建目錄并且在目錄下新建,內(nèi)容為頁面。如果是在內(nèi)置的的情況下,應(yīng)用會自動重啟。 初衷 看了一下spring-boot相關(guān)的書籍,創(chuàng)建一個hello world!的應(yīng)用,是那么的簡單。然而,自己動手,卻很不一樣。 首先,我們只是創(chuàng)建一個簡單的hello world!并...
摘要:結(jié)束語非常智能化,為開發(fā)者提供大量的默認(rèn)配置細(xì)節(jié),因此在的幫助下可以快速完成項(xiàng)目的運(yùn)行,極簡入門繼續(xù)看從零入門系列程序結(jié)構(gòu)設(shè)計說明 環(huán)境準(zhǔn)備 java 開發(fā)環(huán)境 JDK1.8 安裝 Maven 安裝,jar自動依賴及包管理工具 IDE編輯器:IntelliJ IDEA 2019 說明 本項(xiàng)目為從零入門示例,目標(biāo)為構(gòu)建一個書籍增刪改查管理頁,力爭記錄一個無java基礎(chǔ)的程序員學(xué)習(xí)筆...
摘要:二教程環(huán)境三創(chuàng)建項(xiàng)目創(chuàng)建項(xiàng)目有兩種方式一種是在官網(wǎng)上創(chuàng)建二是在上創(chuàng)建如圖所示勾選然后點(diǎn),然后一直默認(rèn)最后點(diǎn)擊完成即可。我們這里看到和普通的接口沒有異同,除了返回類型是用包裝之外。與之對應(yīng)的還有,這個后面我們會講到。 showImg(https://segmentfault.com/img/remote/1460000018819338?w=1024&h=500); 從去年開始就開始學(xué)習(xí)...
閱讀 3606·2021-11-22 15:22
閱讀 3387·2019-08-30 15:54
閱讀 2777·2019-08-30 15:53
閱讀 985·2019-08-29 11:22
閱讀 3603·2019-08-29 11:14
閱讀 2140·2019-08-26 13:46
閱讀 2278·2019-08-26 13:24
閱讀 2345·2019-08-26 12:22