前言:本系列文章非本人原創,轉自:http://tengj.top/2017/04/24/springboot0/html
首先聲明,Spring Boot不是一門新技術。從本質上來講,Spring Boot就是Spring,它作了那些沒有它你也須要去作的Spring Bean配置。它使用「習慣優於配置」(項目中存在大量的配置,此外還內置了一個習慣性的配置,讓你無需手動進行配置)的理念讓你的項目快速運行起來。使用Spring Boot很容易建立一個獨立運行(運行jar,內嵌Servlet容器)、準生產級別的基於Spring框架的項目,使用Spring Boot,你能夠不用或者只須要不多的Spring配置。java
Spring將不少魔法帶入了Spring應用程序的開發之中,其中最重要的是如下四個核心。mysql
Spring Boot 1.5.1.RELEASE須要Java 7和Spring Framework 4.3.6.RELEASE或更高版本,你也可使用Spring Boot with Java 6和一些額外的配置(強烈不建議),使用Maven(3.2+)或Gradle 2(2.9或更高版本)和3來構建。react
雖然你可使用Java 6或7的Spring Boot,但咱們一般推薦Java 8。git
本博客系列統一使用Java 1.8,Spring Boot 1.5.1.RELEASE以及Maven3.3.9版本。開發工具使用IDEA。web
(此處能夠看原博主另一篇介紹IDEA入門文章,內有官方中文教程:Java人員正確使用 IntelliJ IDEA的方式)spring
先看看傳統Spring MVC開發一個簡單的Hello World Web應用程序,須要作哪些事情:sql
這份清單裏面只有一個東西是和Hello World功能相關的,即控制器,剩下的都是Spring開發的Web應用程序必需的通用模板。mongodb
接下來看看Spring Boot如何搞定?很簡單,僅僅只須要很是少的幾個配置就能夠迅速方便的搭建起來一套web項目。apache
構建一個Sping Boot的Maven項目,強烈推薦Spring Initializr,它從本質上來講就是一個Web應用程序,它能爲你生成Spring Boot項目結構。
Spring Initializr有幾種用法:
I.經過Web界面使用
1.訪問:http://start.spring.io/
2.
選擇構建工具Maven Project
、Spring Boot版本1.5.1(原博主的版本,如今版本爲2.1.8)
以及一些工程基本信息,可參考下圖所示
3.點擊Generate Project
下載項目壓縮包
4.導入到你的工程
4.1若是是IDEA,則須要:
a.菜單中選擇File
–>New
–>Project from Existing Sources...
b.選擇解壓後的項目文件夾,點擊OK
c.點擊Import project from external model
並選擇Maven
,點擊Next
到底爲止。
d.若你的環境有多個版本的JDK,注意到選擇Java SDK
的時候請選擇Java 7
以上的版本
4.2若是是Myeclipse,則須要:
a.右鍵選擇Import–>Existing Maven Projects–>選擇項目位置點擊–>finish
b.等待項目自動配置加載
II.經過IntelliJ IDEA使用(原博主推薦,此處不詳細編寫。)
如你所見,項目裏面基本沒有代碼,除了幾個空目錄外,還包含以下幾樣東西。
看看Spring Boot的跟普通Spring MVC工程的Maven配置有什麼區別?
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.8.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.birdboot</groupId> 12 <artifactId>bootDemo</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>bootDemo</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter</artifactId> 25 </dependency> 26 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-web</artifactId> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-test</artifactId> 35 <scope>test</scope> 36 </dependency> 37 </dependencies> 38 39 <build> 40 <plugins> 41 <plugin> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-maven-plugin</artifactId> 44 </plugin> 45 </plugins> 46 </build> 47 48 </project>
Spring Boot父級依賴>
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.1.8.RELEASE</version> 5 <relativePath/> <!-- lookup parent from repository --> 6 </parent>
這塊配置就是Spring Boot父級依賴,有了這個,當前的項目就是Spring Boot項目了,spring-boot-starter-parent是一個特殊的starter,它用來提供相關的Maven默認依賴,使用它以後,經常使用的包依賴能夠省去version標籤。關於Spring Boot提供了哪些jar包的依賴,可查看C:\Users\用戶\.m2\repository\org\springframework\boot\spring-boot-dependencies\2.1.8.RELEASE\spring-boot-dependencies-2.1.8.RELEASE.pom
1 <properties> 2 <activemq.version>5.15.10</activemq.version> 3 <antlr2.version>2.7.7</antlr2.version> 4 <appengine-sdk.version>1.9.76</appengine-sdk.version> 5 <artemis.version>2.6.4</artemis.version> 6 <aspectj.version>1.9.4</aspectj.version> 7 <assertj.version>3.11.1</assertj.version> 8 <atomikos.version>4.0.6</atomikos.version> 9 <bitronix.version>2.1.4</bitronix.version> 10 <build-helper-maven-plugin.version>3.0.0</build-helper-maven-plugin.version> 11 <byte-buddy.version>1.9.16</byte-buddy.version> 12 <caffeine.version>2.6.2</caffeine.version> 13 <cassandra-driver.version>3.6.0</cassandra-driver.version> 14 <classmate.version>1.4.0</classmate.version> 15 <commons-codec.version>1.11</commons-codec.version> 16 <commons-dbcp2.version>2.5.0</commons-dbcp2.version> 17 <commons-lang3.version>3.8.1</commons-lang3.version> 18 <commons-pool.version>1.6</commons-pool.version> 19 <commons-pool2.version>2.6.2</commons-pool2.version> 20 <couchbase-cache-client.version>2.1.0</couchbase-cache-client.version> 21 <couchbase-client.version>2.7.9</couchbase-client.version> 22 <dependency-management-plugin.version>1.0.8.RELEASE</dependency-management-plugin.version> 23 <derby.version>10.14.2.0</derby.version> 24 <dom4j.version>1.6.1</dom4j.version> 25 <dropwizard-metrics.version>4.0.6</dropwizard-metrics.version> 26 <ehcache.version>2.10.6</ehcache.version> 27 <ehcache3.version>3.6.3</ehcache3.version> 28 <elasticsearch.version>6.4.3</elasticsearch.version> 29 <embedded-mongo.version>2.1.2</embedded-mongo.version> 30 <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version> 31 <flatten-maven-plugin.version>1.0.1</flatten-maven-plugin.version> 32 <flyway.version>5.2.4</flyway.version> 33 <freemarker.version>2.3.29</freemarker.version> 34 <git-commit-id-plugin.version>2.2.6</git-commit-id-plugin.version> 35 <glassfish-el.version>3.0.0</glassfish-el.version> 36 <glassfish-jaxb.version>2.3.1</glassfish-jaxb.version> 37 <groovy.version>2.5.8</groovy.version> 38 <gson.version>2.8.5</gson.version> 39 <h2.version>1.4.199</h2.version> 40 <hamcrest.version>1.3</hamcrest.version> 41 <hazelcast.version>3.11.4</hazelcast.version> 42 <hazelcast-hibernate5.version>1.2.3</hazelcast-hibernate5.version> 43 <hibernate.version>5.3.11.Final</hibernate.version> 44 <hibernate-validator.version>6.0.17.Final</hibernate-validator.version> 45 <hikaricp.version>3.2.0</hikaricp.version> 46 <hsqldb.version>2.4.1</hsqldb.version> 47 <htmlunit.version>2.33</htmlunit.version> 48 <httpasyncclient.version>4.1.4</httpasyncclient.version> 49 <httpclient.version>4.5.9</httpclient.version> 50 <httpcore.version>4.4.12</httpcore.version> 51 <infinispan.version>9.4.16.Final</infinispan.version> 52 <influxdb-java.version>2.14</influxdb-java.version> 53 <jackson.version>2.9.9.20190807</jackson.version> 54 <jackson-bom.version>${jackson.version}</jackson-bom.version> 55 <janino.version>3.0.15</janino.version> 56 <javax-activation.version>1.2.0</javax-activation.version> 57 <javax-annotation.version>1.3.2</javax-annotation.version> 58 <javax-cache.version>1.1.1</javax-cache.version> 59 <javax-jaxb.version>2.3.1</javax-jaxb.version> 60 <javax-jaxws.version>2.3.1</javax-jaxws.version> 61 <javax-jms.version>2.0.1</javax-jms.version> 62 <javax-json.version>1.1.4</javax-json.version> 63 <javax-jsonb.version>1.0</javax-jsonb.version> 64 <javax-mail.version>1.6.2</javax-mail.version> 65 <javax-money.version>1.0.3</javax-money.version> 66 <javax-persistence.version>2.2</javax-persistence.version> 67 <javax-transaction.version>1.3</javax-transaction.version> 68 <javax-validation.version>2.0.1.Final</javax-validation.version> 69 <javax-websocket.version>1.1</javax-websocket.version> 70 <jaxen.version>1.1.6</jaxen.version> 71 <jaybird.version>3.0.6</jaybird.version> 72 <jboss-logging.version>3.3.3.Final</jboss-logging.version> 73 <jboss-transaction-spi.version>7.6.0.Final</jboss-transaction-spi.version> 74 <jdom2.version>2.0.6</jdom2.version> 75 <jedis.version>2.9.3</jedis.version> 76 <jersey.version>2.27</jersey.version> 77 <jest.version>6.3.1</jest.version> 78 <jetty.version>9.4.19.v20190610</jetty.version> 79 <jetty-el.version>8.5.40</jetty-el.version> 80 <jetty-jsp.version>2.2.0.v201112011158</jetty-jsp.version> 81 <jetty-reactive-httpclient.version>1.0.3</jetty-reactive-httpclient.version> 82 <jmustache.version>1.14</jmustache.version> 83 <jna.version>4.5.2</jna.version> 84 <joda-time.version>2.10.3</joda-time.version> 85 <johnzon.version>${johnzon-jsonb.version}</johnzon.version> 86 <johnzon-jsonb.version>1.1.13</johnzon-jsonb.version> 87 <jolokia.version>1.6.2</jolokia.version> 88 <jooq.version>3.11.12</jooq.version> 89 <jsonassert.version>1.5.0</jsonassert.version> 90 <json-path.version>2.4.0</json-path.version> 91 <jstl.version>1.2</jstl.version> 92 <jtds.version>1.3.1</jtds.version> 93 <junit.version>4.12</junit.version> 94 <junit-jupiter.version>5.3.2</junit-jupiter.version> 95 <kafka.version>2.0.1</kafka.version> 96 <kotlin.version>1.2.71</kotlin.version> 97 <lettuce.version>5.1.8.RELEASE</lettuce.version> 98 <liquibase.version>3.6.3</liquibase.version> 99 <log4j2.version>2.11.2</log4j2.version> 100 <logback.version>1.2.3</logback.version> 101 <lombok.version>1.18.8</lombok.version> 102 <mariadb.version>2.3.0</mariadb.version> 103 <maven-antrun-plugin.version>1.8</maven-antrun-plugin.version> 104 <maven-assembly-plugin.version>3.1.1</maven-assembly-plugin.version> 105 <maven-clean-plugin.version>3.1.0</maven-clean-plugin.version> 106 <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version> 107 <maven-dependency-plugin.version>3.1.1</maven-dependency-plugin.version> 108 <maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version> 109 <maven-enforcer-plugin.version>3.0.0-M2</maven-enforcer-plugin.version> 110 <maven-failsafe-plugin.version>2.22.2</maven-failsafe-plugin.version> 111 <maven-help-plugin.version>3.1.1</maven-help-plugin.version> 112 <maven-install-plugin.version>2.5.2</maven-install-plugin.version> 113 <maven-invoker-plugin.version>3.1.0</maven-invoker-plugin.version> 114 <maven-jar-plugin.version>3.1.2</maven-jar-plugin.version> 115 <maven-javadoc-plugin.version>3.0.1</maven-javadoc-plugin.version> 116 <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version> 117 <maven-shade-plugin.version>3.2.1</maven-shade-plugin.version> 118 <maven-site-plugin.version>3.7.1</maven-site-plugin.version> 119 <maven-source-plugin.version>3.0.1</maven-source-plugin.version> 120 <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version> 121 <maven-war-plugin.version>3.2.3</maven-war-plugin.version> 122 <micrometer.version>1.1.6</micrometer.version> 123 <mimepull.version>1.9.11</mimepull.version> 124 <mockito.version>2.23.4</mockito.version> 125 <mongodb.version>3.8.2</mongodb.version> 126 <mongo-driver-reactivestreams.version>1.9.2</mongo-driver-reactivestreams.version> 127 <mssql-jdbc.version>6.4.0.jre8</mssql-jdbc.version> 128 <mysql.version>8.0.17</mysql.version> 129 <nekohtml.version>1.9.22</nekohtml.version> 130 <neo4j-ogm.version>3.1.13</neo4j-ogm.version> 131 <netty.version>4.1.39.Final</netty.version> 132 <netty-tcnative.version>2.0.25.Final</netty-tcnative.version> 133 <nio-multipart-parser.version>1.1.0</nio-multipart-parser.version> 134 <pooled-jms.version>1.0.6</pooled-jms.version> 135 <postgresql.version>42.2.6</postgresql.version> 136 <prometheus-pushgateway.version>0.5.0</prometheus-pushgateway.version> 137 <quartz.version>2.3.1</quartz.version> 138 <querydsl.version>4.2.1</querydsl.version> 139 <rabbit-amqp-client.version>5.4.3</rabbit-amqp-client.version> 140 <reactive-streams.version>1.0.3</reactive-streams.version> 141 <reactor-bom.version>Californium-SR11</reactor-bom.version> 142 <rest-assured.version>3.1.1</rest-assured.version> 143 <rxjava.version>1.3.8</rxjava.version> 144 <rxjava2.version>2.2.12</rxjava2.version> 145 <rxjava-adapter.version>1.2.1</rxjava-adapter.version> 146 <saaj-impl.version>1.5.0</saaj-impl.version> 147 <selenium.version>3.14.0</selenium.version> 148 <selenium-htmlunit.version>2.33.0</selenium-htmlunit.version> 149 <sendgrid.version>4.3.0</sendgrid.version> 150 <servlet-api.version>4.0.1</servlet-api.version> 151 <slf4j.version>1.7.28</slf4j.version> 152 <snakeyaml.version>1.23</snakeyaml.version> 153 <solr.version>7.7.2</solr.version> 154 <spring.version>5.1.9.RELEASE</spring.version> 155 <spring-amqp.version>2.1.8.RELEASE</spring-amqp.version> 156 <spring-batch.version>4.1.2.RELEASE</spring-batch.version> 157 <spring-cloud-connectors.version>2.0.6.RELEASE</spring-cloud-connectors.version> 158 <spring-data-releasetrain.version>Lovelace-SR10</spring-data-releasetrain.version> 159 <spring-framework.version>${spring.version}</spring-framework.version> 160 <spring-hateoas.version>0.25.2.RELEASE</spring-hateoas.version> 161 <spring-integration.version>5.1.7.RELEASE</spring-integration.version> 162 <spring-kafka.version>2.2.8.RELEASE</spring-kafka.version> 163 <spring-ldap.version>2.3.2.RELEASE</spring-ldap.version> 164 <spring-plugin.version>1.2.0.RELEASE</spring-plugin.version> 165 <spring-restdocs.version>2.0.3.RELEASE</spring-restdocs.version> 166 <spring-retry.version>1.2.4.RELEASE</spring-retry.version> 167 <spring-security.version>5.1.6.RELEASE</spring-security.version> 168 <spring-session-bom.version>Bean-SR7</spring-session-bom.version> 169 <spring-ws.version>3.0.7.RELEASE</spring-ws.version> 170 <sqlite-jdbc.version>3.25.2</sqlite-jdbc.version> 171 <statsd-client.version>3.1.0</statsd-client.version> 172 <sun-mail.version>${javax-mail.version}</sun-mail.version> 173 <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> 174 <thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version> 175 <thymeleaf-extras-java8time.version>3.0.4.RELEASE</thymeleaf-extras-java8time.version> 176 <thymeleaf-extras-springsecurity.version>3.0.4.RELEASE</thymeleaf-extras-springsecurity.version> 177 <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version> 178 <tomcat.version>9.0.24</tomcat.version> 179 <unboundid-ldapsdk.version>4.0.11</unboundid-ldapsdk.version> 180 <undertow.version>2.0.26.Final</undertow.version> 181 <versions-maven-plugin.version>2.7</versions-maven-plugin.version> 182 <webjars-hal-browser.version>3325375</webjars-hal-browser.version> 183 <webjars-locator-core.version>0.35</webjars-locator-core.version> 184 <woodstox.version>5.0.3</woodstox.version> 185 <wsdl4j.version>1.6.3</wsdl4j.version> 186 <xml-apis.version>1.4.01</xml-apis.version> 187 <xml-maven-plugin.version>1.0.2</xml-maven-plugin.version> 188 <xmlunit2.version>2.6.3</xmlunit2.version> 189 </properties>
如果不想使用某個依賴默認的版本,能夠經過覆蓋本身的項目中的屬性來覆蓋各個依賴項,例如,要升級到另外一個Spring Data版本系列,您能夠將如下內容添加到pom.xml中。
1 <properties> 2 <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> 3 </properties>
本來默認版本是Lovelace-SR10的(看上面標紅一行有說明Lovelace-SR10),如今就使用Fowler-SR2版本了。並非每一個人都喜歡繼承自spring-boot-starter-parent POM。您可能有您須要使用的本身的公司標準parent,或者您可能更喜歡顯式聲明全部的Maven配置。
若是不想使用spring-boot-starter-parent,您仍然能夠經過使用scope = import依賴關係來保持依賴關係管理:
1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <!-- Import dependency management from Spring Boot --> 5 <groupId>org.springframework.boot</groupId> 6 <artifactId>spring-boot-dependencies</artifactId> 7 <version>2.1.8.RELEASE</version> 8 <type>pom</type> 9 <scope>import</scope> 10 </dependency> 11 </dependencies> 12 </dependencyManagement>
該設置不容許您使用如上所述的屬性(properties)覆蓋各個依賴項,要實現相同的結果,您須要在spring-boot-dependencies項以前的項目的dependencyManagement中添加一個配置,例如,要升級到另外一個Spring Data版本系列,您能夠將如下內容添加到pom.xml中。
1 <dependencyManagement> 2 <dependencies> 3 <!-- Override Spring Data release train provided by Spring Boot --> 4 <dependency> 5 <groupId>org.springframework.data</groupId> 6 <artifactId>spring-data-releasetrain</artifactId> 7 <version>Fowler-SR2</version> 8 <scope>import</scope> 9 <type>pom</type> 10 </dependency> 11 <dependency> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-dependencies</artifactId> 14 <version>2.1.8.RELEASE</version> 15 <type>pom</type> 16 <scope>import</scope> 17 </dependency> 18 </dependencies> 19 </dependencyManagement>
Spring Boot提供了不少」開箱即用「的依賴模塊,都是以spring-boot-starter-xx做爲命名的。
舉個例子來講明一下這個起步依賴的好處,好比組裝臺式機和品牌機,本身組裝的話須要本身去選擇不一樣的零件,最後還要組裝起來,期間有可能會遇到零件不匹配的問題。耗時又消力,而品牌機就好一點,買來就能直接用的,後續想換零件也是能夠的。相比較之下,後者帶來的效果更好點(這裏就不討論價格問題了哈),起步依賴就像這裏的品牌機,自動給你封裝好了你想要實現的功能的依賴。就好比咱們以前要實現web功能,引入了spring-boot-starter-web這個起步依賴。咱們來看看spring-boot-starter-web到底依賴了哪些,以下圖(這裏採用的是原博主的圖):
看來依賴了好多,因此Spring Boot經過提供衆多起步依賴下降項目依賴的複雜度。起步依賴本質上是一個Maven項目對象模型(Project Object Model,POM),定義了對其餘庫的傳遞依賴,這些東西加在一塊兒即支持某項功能。不少起步依賴的命名都暗示了它們提供的某種或者某類功能。
1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-maven-plugin</artifactId> 6 </plugin> 7 </plugins> 8 </build>
上面的配置就是Spring Boot Maven插件,Spring Boot Maven插件提供了許多方便的功能:
BootDemoApplication是一個很關鍵的啓動類,程序的入口就是這裏,爲了演示簡單,咱們再也不新建控制類,而是直接在這個入口類中編寫,添加@RestController以及index方法,以下:
1 package com.birdboot.bootDemo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @RestController 9 @SpringBootApplication 10 public class BootDemoApplication { 11 12 @RequestMapping("/") 13 public String index(){ 14 return "Hello Spring Boot"; 15 } 16 17 public static void main(String[] args) { 18 SpringApplication.run(BootDemoApplication.class, args); 19 } 20 21 }
main方法這是一個標準的Java應用的main的方法,主要做用是做爲項目啓動的入口。
@RestController註解等價於@Controller+@ResponseBody的結合,使用這個註解的類裏面的方法都以json格式輸出。
最後,啓動項目有三種方式:
First、SpringBootApplication的main方法
Second、使用命令 mvn spring-boot:run」在命令行啓動該應用。
Third、運行「mvn package」進行打包時,會打包成一個能夠直接運行的 JAR 文件,使用「java -jar」命令就能夠直接運行。
打開瀏覽器訪問http://localhost:8080, 就能看到頁面顯示Hello Spring Boot效果了,一個簡單的Web的項目就是如此簡單。
上面講解了那麼多,可是實際開發步驟就是那麼簡單暴力,初始化一個Spring Boot,添加一個控制類,啓動就能看到效果了。
本章做爲入門Spring Boot的入門介紹,相關的須要理解的概念就只有這幾點:
此處記錄本人的一些踩過的坑。
1.本身要在pom.xml文件中增長spring-boot-starter-web的依賴;不然@RestController沒法引入
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency>
2.我採用的Spring boot版本是2.1.8,因此jdk版本要求必須是1.8以上;jdk1.7沒法啓動項目,會報java.lang.UnsupportedClassVersionError的錯誤。
如果出現這個問題,能夠參考:https://blog.csdn.net/w405722907/article/details/77160220