介紹
當您需要加快開發(fā)工作而不必擔心在應用程序中的代碼片段的每次更改時重新啟動服務器時,可能會出現(xiàn)這種情況。Spring Boot 提供了這樣的規(guī)定,您可以使用它輕松實現(xiàn)目標。
每當 classpath 上的文件發(fā)生更改時,使用spring-boot-devtools 的應用程序?qū)⒆詣又匦聠?。?IDE 中工作時,這可能是一個有用的功能,因為它為代碼更改提供了非常快速的反饋循環(huán)。默認情況下,將監(jiān)視指向文件夾的類路徑上的任何條目的更改。
使用此依賴項保存的任何更改,嵌入式 tomcat 將重新啟動。Spring Boot 有一個開發(fā)者工具(devtools)模塊有助于提高開發(fā)者的生產(chǎn)力。
先決條件
Spring Boot 2.2.2 – 2.4.5,Spring Boot Devtools,Java 至少 8
無需重新啟動服務器即可重新加載代碼更改
讓我們在 Eclipse IDE 中創(chuàng)建 Spring Boot 項目。該項目的名稱是spring-boot-reload-changes-without-server-restart。
該的build.gradle下面的腳本給出與基于gradle這個項目所需的依賴關(guān)系:
buildscript {
ext {
springBootVersion = '2.2.2.RELEASE' to 2.4.5
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java-library'
id 'org.springframework.boot' version "${springBootVersion}"
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
implementation("org.springframework.boot:spring-boot-devtools:${springBootVersion}")
}
下面給出了基于 maven 的項目對應的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>com.roytuts</groupId>
<artifactId>spring-boot-reload-changes-without-server-restart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
<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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
現(xiàn)在我將編寫主類來啟動 Spring Boot 應用程序:
package com.roytuts.spring.boot.reload.changes.without.server.restart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootChangesWithoutRestartServerApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootChangesWithoutRestartServerApp.class, args);
}
}
現(xiàn)在運行上面的主類將部署您的應用程序,服務器將在端口 8080 上啟動。
現(xiàn)在嘗試訪問根 URL http://localhost:8080或http://localhost:8080/greet,您將在瀏覽器上看到以下輸出:
接下來,我將使用/greet端點創(chuàng)建以下 Spring REST 控制器類。
package com.roytuts.spring.boot.reload.changes.without.server.restart;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return "Greetings, Welcome!";
}
}
現(xiàn)在,每次保存代碼更改時,嵌入式 Tomcat 服務器都會自動重新初始化。
現(xiàn)在點擊 URL http://localhost:8080/greet會給你以下輸出:
所以你知道如何在不重啟服務器的情況下在 Spring Boot 應用程序中重新加載我們的代碼更改。