使用Spring Boot開發者工具進行自動重啟和頁面自動刷新
大家可能都聽說過開發Node.js應用時可以使用多種工具對開發者提供便利,如WebPack提供了開發者服務器來支持js應用動態更替,並在保存文件時自動刷新瀏覽器。Spring Boot也提供了相似的開發者工具,讓我們更快速、更舒心的開發Spring Boot應用。大家看完本教程就可以學會如何如用Spring Boot開發者工具進行自動重啟和自動刷新頁面。
自動重啟原理
Spring Boot的開發者工具會為應用創建兩個classloader。一個是用來加載不會變動的類,稱為base classloader。另一個是restart classloader,用來加載經常變動的類,默認情況下Spring Boot開發者工具會監控classpath下所有的類。當有類變動時,舊的restart classloader就會被丟棄,然後再創建一個新的,以此來加快重啟速度。
基礎環境
- JDK 1.8
- Maven 3.3.9
- IntelliJ 2018.1
- Git
項目源碼
Gitee碼雲
創建項目
使用IntelliJ創建一個maven項目:
- groupdId: zxuqian.cn
- artifactId: devtools
使用如下pom.xml
文件配置:
<?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>cn.zxuqian</groupId> <artifactId>devtools</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>devtools</name> <description>Showcase project for Spring Boot developer tools</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.BUILD-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
這裏我們用了最新的snapshop版本的spring boot 2.1.0,然後添加spring-boot-devtools
依賴,並把它設置為optional
的,那麽這樣在最後打包的產品環境中,devtools將不會被打包進來。
接下來添加一個測試用的控制器cn.zxuqian.devtools.controller.HelloController
:
package cn.zxuqian.devtools.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/") public String hello() { return "hello world"; } }
然後創建cn.zxuqian.devtools.DevtoolsApplication
類配置Spring Boot應用:
package cn.zxuqian.devtools;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DevtoolsApplication {
public static void main(String[] args) {
SpringApplication.run(DevtoolsApplication.class, args);
}
}
安裝LiveReload插件
從LiveReload官網下載LiveReload的Chrome或Firefox或Safari瀏覽器插件,然後啟用此插件。
測試
使用spring-boot:run
插件啟動此應用,在瀏覽器打開http://localhost:8080
會看到hello world
字樣。然後在我們的控制器中把返回值修改一下,如改為:Hola!
,在IntelliJ中,我們必須要執行Build->Build Project
才能重新編譯新改動的代碼,我們也可以用快捷鍵command + (fn) + F9
mac下,來執行編譯。稍等幾秒就會看到瀏覽器自動刷新為修改後的值了。
歡迎訪問我的博客:張旭乾的博客
使用Spring Boot開發者工具進行自動重啟和頁面自動刷新