Spring Boot Hello World的實(shí)現(xiàn)代碼
本篇文章是SpringBoot最入門的介紹。我們不借助任何額外的工具,從無到有創(chuàng)建一個(gè)Spring Boot的web項(xiàng)目,并運(yùn)行這個(gè)項(xiàng)目。
項(xiàng)目構(gòu)建
歸根結(jié)底,Spring Boot就只是一個(gè)框架,幾個(gè)jar而已,沒什么神奇的。但使用Spring Initializr創(chuàng)建項(xiàng)目的過程把很多信息屏蔽掉了,這樣我們就很難搞清楚Spring Boot的本質(zhì)是什么。下面僅使用maven從無到有構(gòu)建一個(gè)Spring Boot的web項(xiàng)目。
先創(chuàng)建一個(gè)maven空工程如下所示,項(xiàng)目的名字叫spring-boot-hello。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.poype</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> </dependencies> </project>
現(xiàn)在這還是一個(gè)空的maven項(xiàng)目,我們可以在dependencies標(biāo)簽中添加我們需要的依賴,例如添加Spring Boot的依賴。但是Spring Boot為了減少配置,方便我們開發(fā),提供了一個(gè)parent maven工程spring-boot-starter-parent,我們只要讓我們的這個(gè)項(xiàng)目繼承spring-boot-starter-parent工程,就能減少好多配置。修改我們的POM配置如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.poype</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> </dependencies> </project>
目前我們的這個(gè)maven項(xiàng)目還沒有導(dǎo)入任何dependency,這點(diǎn)可以通過執(zhí)行mvn dependency:tree
命令確定。
我們要?jiǎng)?chuàng)建的是一個(gè)web項(xiàng)目,所以添加spring-boot-starter-web這個(gè)依賴。修改POM配置如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.poype</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
由于在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這個(gè)依賴后,有許多的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)完成,新建一個(gè)HelloController測(cè)試類,輸入如下代碼:
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)完成了一個(gè)簡(jiǎn)單的web應(yīng)用開發(fā),可以啟動(dòng)我們這個(gè)應(yīng)用了。
由于我們的工程繼承了spring-boot-starter-parent的POM配置,它提供了啟動(dòng)spring-boot應(yīng)用的相關(guān)插件(該插件的run目標(biāo)用于啟動(dòng)應(yīng)用),可以通過執(zhí)行mvn spring-boot:run
命令啟動(dòng)應(yīng)用,得到如下運(yùn)行結(jié)果。
從運(yùn)行結(jié)果中可以看到,spring-boot啟動(dòng)了Tomcat服務(wù)器,并監(jiān)聽在8080端口。下面我們打開瀏覽器,輸入地址http://localhost:8080/hello
就可以看到程序運(yùn)行結(jié)果。
應(yīng)用打包
應(yīng)用構(gòu)建好之后,需要build出一個(gè)應(yīng)用包才能用于生產(chǎn)部署。為此需要在POM配置中新增一個(gè)插件,修改POM配置如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.poype</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
接著我們執(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
命令就可以啟動(dòng)應(yīng)用了。
Spring Boot是怎么做到的
上面的例子雖然非常簡(jiǎn)單,但卻也是一個(gè)標(biāo)準(zhǔn)的spring web應(yīng)用。我們可以回憶一下,如果沒有Spring Boot,創(chuàng)建一個(gè)這樣的web應(yīng)用都需要哪些步驟呢?首先要在maven的POM中導(dǎo)入N多相關(guān)dependency(包括Spring的、servlet的、json的...)。然后添加各種復(fù)雜配置(包括servlet的、Spring的、Spring MVC的...)。最后寫完代碼build好一個(gè)war包,我們還需要下載一個(gè)Tomcat,并將war包放到tomcat下的指定路徑,啟動(dòng)tomcat部署應(yīng)用。這個(gè)過程即使是工作幾年的老司機(jī),從無到有創(chuàng)建一個(gè)項(xiàng)目估計(jì)也要十幾分鐘,如果是新手再遇到一些問題,解決起來就更麻煩了,可能幾個(gè)小時(shí)也不一定能搞得出來。
使用Spring Boot構(gòu)建應(yīng)用,即便我們僅僅使用maven,也幾乎沒有什么配置。如果使用Spring Initializr的話,創(chuàng)建好工程無需任何配置就直接可以寫代碼了,非常的方便,即使是新手幾分鐘也能搞出來這個(gè)HelloWorld應(yīng)用。這就是Spring Boot給我的最初印象,但是它是如何做到這些的呢?
約定大于配置
Spring Boot提供了很多Starter依賴,每種類型的Starter提供了這種類型應(yīng)用可能需要的一系列dependency(利用maven間接依賴的特性)。例如我們這里創(chuàng)建的是一個(gè)web應(yīng)用,所以我們的項(xiàng)目依賴spring-boot-starter-web,而spring-boot-starter-web會(huì)將web開發(fā)可能需要的依賴全部幫我們導(dǎo)入,省去很多配置的工作。spring-boot-starter-parent是一個(gè)特殊的starter,它提供了許多maven默認(rèn)配置,如dependenceManagment。
另一個(gè)比較重要的是注解@EnableAutoConfiguration
,Spring Boot看到這個(gè)注解,會(huì)根據(jù)已經(jīng)加入的jar dependency執(zhí)行相關(guān)的配置。例如在我們的工程中有Spring MVC和Tomcat的依賴,Spring Boot就會(huì)猜到這是一個(gè)WEB工程,它就會(huì)對(duì)項(xiàng)目執(zhí)行相應(yīng)的配置(如Spring MVC和Servlet的配置)。
應(yīng)用啟動(dòng)
Spring Boot自帶了一個(gè)Tomcat容器,省去我們自己安裝和配置容器的工作。為了了解Spring Boot的啟動(dòng)過程,我們將build好的jar解壓得到的目錄結(jié)構(gòu)如下圖所示:
其中最主要的配置文件是MANIFEST.MF,在執(zhí)行java -jar *.jar啟動(dòng)命令時(shí),JVM參考的就是這個(gè)文件的配置。其文件內(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包中的啟動(dòng)類,可以看到是一個(gè)叫org.springframework.boot.loader.JarLauncher類,是Spring Boot提供的Launcher類。Start-Class是我們自己編寫的含有main方法的類。Spring-Boot-Classes是應(yīng)用自己類的路徑,Spring-Boot-Lib是應(yīng)用依賴的第三方包的路徑。
看到這里我們可以大概總結(jié)一下(僅僅是YY,目前理解還不夠深入),Spring Boot實(shí)現(xiàn)了一套自己的部署路徑規(guī)范(應(yīng)用自己的類放在哪里,應(yīng)用依賴的第三方j(luò)ar放在哪里等等),就像J2EE規(guī)范一樣。然后利用tomcat的jar實(shí)現(xiàn)servlet容器的功能,對(duì)WEB請(qǐng)求進(jìn)行處理。
可以說Spring Boot利用tomcat打造了一個(gè)全新的平臺(tái),這個(gè)平臺(tái)也僅僅只在servlet容器部分利用到了tomcat的功能,至于部署規(guī)范和加載機(jī)制,都是Spring Boot自己全新實(shí)現(xiàn)的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis 實(shí)現(xiàn)數(shù)據(jù)的批量新增和刪除的操作
這篇文章主要介紹了MyBatis 實(shí)現(xiàn)數(shù)據(jù)的批量新增和刪除的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java的Struts框架中的if/else標(biāo)簽使用詳解
這篇文章主要介紹了Java的Struts框架中的if/else標(biāo)簽使用詳解,Struts是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12Java形參和實(shí)參的實(shí)例之Integer類型與Int類型用法說明
這篇文章主要介紹了Java形參和實(shí)參的實(shí)例之Integer類型與Int類型用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10java中字符串轉(zhuǎn)整數(shù)及MyAtoi方法的實(shí)現(xiàn)
這篇文章主要介紹了java中字符串轉(zhuǎn)整數(shù)及MyAtoi方法的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-05-05Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序
最近在學(xué)習(xí)Java,然后上個(gè)月迷上了九宮格數(shù)獨(dú),玩了幾天,覺得實(shí)在有趣,就想著能不能用編程來解決,于是就自己寫了個(gè),還真解決了。下面這篇文章就給大家主要介紹了Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序,需要的朋友可以參考借鑒。2017-01-01