技術介紹 devtools:是boot的一個熱部署工具,當我們修改了classpath下的文件(包括類文件、屬性文件、頁面等)時,會重新啟動應用(由於其採用的雙類載入器機制,這個啟動會非常快,如果發現這個啟動比較慢,可以選擇使用jrebel) 雙類載入器機制:boot使用了兩個類載入器來實現重啟(r ...
技術介紹
- devtools:是boot的一個熱部署工具,當我們修改了classpath下的文件(包括類文件、屬性文件、頁面等)時,會重新啟動應用(由於其採用的雙類載入器機制,這個啟動會非常快,如果發現這個啟動比較慢,可以選擇使用jrebel)
- 雙類載入器機制:boot使用了兩個類載入器來實現重啟(restart)機制:base類載入器(簡稱bc)+restart類載入器(簡稱rc)。
- bc:用於載入不會改變的jar(eg.第三方依賴的jar)
- rc:用於載入我們正在開發的jar(eg.整個項目里我們自己編寫的類)。當應用重啟後,原先的rc被丟掉、重新new一個rc來載入這些修改過的東西,而bc卻不需要動一下。這就是devtools重啟速度快的原因。
- 雙類載入器機制:boot使用了兩個類載入器來實現重啟(restart)機制:base類載入器(簡稱bc)+restart類載入器(簡稱rc)。
- thymeleaf:boot推薦的模板引擎,這裡做簡要的介紹,用來介紹devtools對頁面的熱部署。
項目結構:
1、pom.xml
1 <!-- thymeleaf --> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-thymeleaf</artifactId> 5 </dependency> 6 <!-- 7 devtools可以實現頁面熱部署(即頁面修改後會立即生效,這個可以直接在application.properties文件中配置spring.thymeleaf.cache=false來實現), 8 實現類文件熱部署(類文件修改後不會立即生效),實現對屬性文件的熱部署。 9 即devtools會監聽classpath下的文件變動,並且會立即重啟應用(發生在保存時機),註意:因為其採用的虛擬機機制,該項重啟是很快的 10 --> 11 <dependency> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-devtools</artifactId> 14 <optional>true</optional><!-- optional=true,依賴不會傳遞,該項目依賴devtools;之後依賴myboot項目的項目如果想要使用devtools,需要重新引入 --> 15 </dependency>View Code
說明:如果僅僅使用thymeleaf,只需要引入thymeleaf;如果需要使用devtools,只需要引入devtools。
註意:
- maven中的optional=true表示依賴不會傳遞。即此處引用的devtools不會傳遞到依賴myboot項目的項目中。
- 僅僅加入devtools在我們的eclipse中還不起作用,這時候還需要對之前添加的spring-boot-maven-plugin做一些修改,如下:
1 <!-- 用於將應用打成可直接運行的jar(該jar就是用於生產環境中的jar) 值得註意的是,如果沒有引用spring-boot-starter-parent做parent, 2 且採用了上述的第二種方式,這裡也要做出相應的改動 --> 3 <plugin> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-maven-plugin</artifactId> 6 <configuration> 7 <fork>true</fork><!-- 如果沒有該項配置,肯呢個devtools不會起作用,即應用不會restart --> 8 </configuration> 9 </plugin>
View Code即添加了fork:true
2、ThymeleafController
1 package com.xxx.firstboot.web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 import org.springframework.web.bind.annotation.RequestParam; 8 9 import io.swagger.annotations.Api; 10 import io.swagger.annotations.ApiOperation; 11 12 @Api("測試Thymeleaf和devtools") 13 @Controller 14 @RequestMapping("/thymeleaf") 15 public class ThymeleafController { 16 17 @ApiOperation("第一個thymeleaf程式") 18 @RequestMapping(value = "/greeting", method = RequestMethod.GET) 19 public String greeting(@RequestParam(name = "name", required = false, defaultValue = "world") String name, 20 Model model) { 21 model.addAttribute("xname", name); 22 return "greet"; 23 } 24 25 }View Code
說明:Model可以作為一個入參,在代碼中,將屬性以"key-value"的形式存入model,最後直接返回字元串即可。
3、greet.html
1 <!DOCTYPE HTML> 2 <html xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <title>第一個thymeleaf程式</title> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 </head> 7 <body> 8 <p th:text="'Hello, ' + ${xname} + '!'" /> 9 <div>1234567890!!!xx</div> 10 </body> 11 </html>View Code
註意:
- src/main/resources/templates:頁面存放目錄
- src/main/resources/static:方式靜態文件(css、js等)
以上的目錄與ssm中開發的不一樣,ssm中會放在src/main/webapp下
測試:
- 修改類-->保存:應用會重啟
- 修改配置文件-->保存:應用會重啟
- 修改頁面-->保存:應用會重啟,頁面會刷新(原理是將spring.thymeleaf.cache設為false)
補充:
- 預設情況下,/META-INF/maven,/META-INF/resources,/resources,/static,/templates,/public這些文件夾下的文件修改不會使應用重啟,但是會重新載入(devtools內嵌了一個LiveReload server,當資源發生改變時,瀏覽器刷新)。
- 如果想改變預設的設置,可以自己設置不重啟的目錄:spring.devtools.restart.exclude=static/**,public/**,這樣的話,就只有這兩個目錄下的文件修改不會導致restart操作了。
- 如果要在保留預設設置的基礎上還要添加其他的排除目錄:spring.devtools.restart.additional-exclude
- 如果想要使得當非classpath下的文件發生變化時應用得以重啟,使用:spring.devtools.restart.additional-paths,這樣devtools就會將該目錄列入了監聽範圍。