我做好了從入門到放棄的准備,你卻告訴我炒雞簡單 —— Java面試必修
引言
SpringBoot是由Pivotal團隊提供的全新框架,從最根本上來講,Spring Boot就是簡化開發人員從0構建項目的繁瑣步驟,巧妙的封裝了很多插件模塊,讓開發人員不再擔心版本依賴或復雜的三方依賴問題,它能夠被任意項目的構建系統所使用。
入門項目
接下來,我們什么都先不談,本文着重介紹SpringBoot簡單配置與服務搭建,預計花費您5分鍾的閱讀時間,動起來吧,非常非常簡單噢。
工具
SpringBoot版本:2.0.4
開發工具:IDEA 2018
Maven:3.3 9
JDK:1.8
項目快速建立方式一:
首先我們在SPRING INITIALIZR 上建一個簡單項目,並導入到IDEA中,如下圖:






項目快速建立方式二(常用):
步驟 File
—>New
—>Project



下一步,然后直接完成,選擇new window
即可

工程結構
DemoApplication.java
:應用程序啟動入口,可直接Run啟動服務,類似於tomcat的start.shDemoApplicationTests.java
:Junit測試類,已自動注入加載了SpringBoot容器的上下文application.properties
:配置屬性空文件,可改為application.yml文件,SpringBoot都能識別pom.xml
:maven工程定義文件,表明該項目的maven坐標信息
疑問解析
- 構建項目時為何選擇了
Spring Initializr
?
答:spring initializr
是Spring 官方提供的一個很好的工具,用來初始化一個Spring boot 的項目 spring initializr
有兩種用法。一是在官網創建然后導入到編輯器,二是直接File->New->Project
SpringBoot 之pom.xml
以下簡稱xml,xml中與普通maven項目的xml無太多差異,如下:

pom差異解析
差異一. 引入了該parent說明具備了SpringBoot的基本功能,可直接依賴其父工程(SpringBoot)的包,如差異二(無需聲明版本號)
差異二. web應用啟動核心jar,解壓出來里面除了些依賴什么都沒有,所以Starter主要用來簡化依賴用的,比如我們之前做MVC時要引入日志組件,那么需要去找到log4j的版本,然后引入,現在有了Starter之后,直接用這個之后,log4j就自動引入了,也不用關心版本這些問題,注:若想更改其下某一個jar(如log4j)的版本,則可自行進行升降
差異三. 能夠將Spring Boot應用打包為可執行的jar或war文件,然后以通常的方式運行Spring Boot應用
獨特實現(不常用)
如果你不想使用spring-boot-starter-parent
,或您自己有一套parent依賴標准,您仍然可以通過使用scope = import依賴關系來保持依賴關系管理:
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
該設置不允許您使用如上所述的屬性(properties)覆蓋各個依賴項,要實現相同的結果,您需要在spring-boot-dependencies
項之前的項目的dependencyManagement
中添加一個配置,例如,要升級到另一個Spring Data版本系列,您可以將以下內容添加到自己的pom.xml中。
<dependencyManagement> <dependencies> <!-- Override Spring Data release train provided by Spring Boot --> <!--Spring Data版本更改至Kay-SR9 |變更部分 start--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Kay-SR9</version> <scope>import</scope> <type>pom</type> </dependency> <!--注意:需啊喲在spring-boot-dependencies之前加入需更改的 |變更部分 end --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
此處詳見官方文檔:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml-shortcomings 模塊:13.2.2 Using Spring Boot without the Parent POM
常用依賴模塊
Spring Boot提供了很多已封裝好的模塊,類似於插件,拿來即用,大多都是spring-boot-starter-xx風格,如果要用直接引入即可,就像組裝電腦,組裝i3還是裝i5的CPU看你 自己,下面我們隨便舉例幾個:
<!--快速web應用開發--> <artifactId>spring-boot-starter-web</artifactId> <!--redis緩存服務--> <artifactId>spring-boot-starter-redis</artifactId> <!--應用日志--> <artifactId>spring-boot-starter-logging</artifactId> <!--容器層約定和定制--> <artifactId>spring-boot-starter-jetty</artifactId> <artifactId>spring-boot-starter-undertow</artifactId> <!--數據庫訪問--> <artifactId>spring-boot-starter-jdbc</artifactId> <!--面向切面--> <artifactId>spring-boot-starter-aop</artifactId> <!--應用安全--> <artifactId>spring-boot-starter-security</artifactId>
應用演示
以我們剛剛新建的DemoApplication.java
為例
1.pom.xml文件加入web服務插件(呀,是誰這么聰明,以前弄個springmvc一套下來10來個jar,現在只管一個了)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
- 我們直接在注解上面加入
@RestController
,並且加入一個RequestMapping
方法,啟動服務器之后,我們訪問這個方法即可看到效果
package com.ron.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class DemoApplication { @RequestMapping("/index") public String index(){ return "Hello Spring Boot"; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@RestController
注解等價於@Controller+@ResponseBody
的結合,使用這個注解的類里面的方法都以json格式輸出@SpringBootApplication
是Sprnig Boot項目的核心注解,主要目的是開啟自動配置。后續講解原理的時候深入介紹。- main方法這是一個標准的Java應用的main的方法,主要作用是作為項目啟動的入口。
run運行

打開瀏覽器訪問

單元測試場景
找到項目目錄了src/test/下的測試入口,編寫簡單的http請求來測試;使用mockmvc進行,利用MockMvcResultHandlers.print()
打印出執行結果。
package com.ron.demo; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { private MockMvc mvc; @Before public void setUp() throws Exception { <!--此處為需要測試的Controller類--> mvc = MockMvcBuilders.standaloneSetup(new DemoApplication()).build(); } @Test public void contextLoads() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/index").accept(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.status().isOk()) .andDo(MockMvcResultHandlers.print()) .andReturn(); } }
直接在DemoApplicationTests
中 Ctrl+Shift+F10運行即可看到如下運行結果,若報錯請仔細檢查@Before方法

熱部署配置(會重啟)
工欲善其事,必先利其器。在開發的時候,難免會反復進行修改調試,就目前而言,修改了代碼后是無法直接編譯生效,所以需要我們添加以下依賴,添加后一定要確保已經依賴噢
- 添加如下依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
2.plugin中加入如下
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--加入部分 start--> <configuration> <fork>true</fork> </configuration> <!--加入部分 end--> </plugin> </plugins> </build>
-
第三步修改IDE
settings

設置完成后重啟IDEA即可,本操作在修改代碼之后只會做到自動啟動服務
總結
會使用SpringBoot之后,老板再也不用擔心我寫代碼的速度,總結下來就是簡單、快速、方便!平時如果我們需要搭建一個spring web項目的時候准備依賴包都要很大一部分時間,現在都不用啦。
作者有話說:喜歡的話就請移步Java面試必修網 https://www.itmsbx.com ,請自備水,更多干、干、干貨等着你