知識庫 / Spring / Spring Boot RSS 訂閱

Spring Boot DevTools 概覽

Spring Boot
HongKong
6
02:39 PM · Dec 06 ,2025

1. 簡介

Spring Boot 賦予我們快速搭建和運行服務的能力。

為了進一步提升開發體驗,Spring 發佈了 spring-boot-devtools 工具 – 作為 Spring Boot-1.3 的一部分。本文將嘗試涵蓋我們使用新功能可以實現的好處。

我們將涵蓋以下主題:

  • 屬性默認值
  • 自動重啓
  • 熱部署
  • 全局設置
  • 遠程應用程序

1.1. 在項目添加 Spring-Boot-Devtools

在項目添加 spring-boot-devtools 極其簡單,就像添加任何其他 Spring Boot 模塊一樣。 在現有的 Spring Boot 項目中,添加以下依賴項:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

進行項目的一次性清理構建,並與 spring-boot-devtools 集成。最新版本可在此處找到:https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools

2. 默認屬性

Spring Boot 會進行大量的自動配置,包括默認啓用緩存以提高性能。其中一個例子是使用模板引擎(例如 thymeleaf)緩存模板。但在開發過程中,儘快看到更改更為重要。

可以使用 spring.thymeleaf.cache=false 屬性在 application.properties 文件中禁用 thymeleaf 的默認緩存行為。由於 spring-boot-devtools 會自動為我們處理此項,因此無需手動進行操作。

3. 自動重啓

在典型的應用程序開發環境中,開發者會進行一些修改,構建項目並部署/啓動應用程序以使新更改生效,或者嘗試利用 JRebel 等工具。

藉助 spring-boot-devtools,這個過程也得到了自動化。當 classpath 中的文件發生更改時,使用 spring-boot-devtools 的應用程序會自動重啓。該功能的優勢在於,驗證更改所需的時間大大減少。

19:45:44.804 ... - Included patterns for restart : []
19:45:44.809 ... - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
19:45:44.810 ... - Matching URLs for reloading : [file:/.../target/test-classes/, file:/.../target/classes/]

 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:45:45.174  ...: Starting Application on machine with PID 7724 (<some path>\target\classes started by user in <project name>)
2017-03-12 19:45:45.175  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:45:45.510  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy

正如日誌所示,啓動該應用程序的線程不是一個main線程,而是一個restartedMain線程。項目中的任何更改,例如 Java 文件更改,都會自動重啓項目:

2017-03-12 19:53:46.204  ...: Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy
2017-03-12 19:53:46.208  ...: Unregistering JMX-exposed beans on shutdown


 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:53:46.587  ...: Starting Application on machine with PID 7724 (<project path>\target\classes started by user in <project name>)
2017-03-12 19:53:46.588  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:53:46.591  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@acaf4a1: startup date [Sun Mar 12 19:53:46 IST 2017]; root of context hierarchy

4. 實時刷新 (Live Reload)

spring-boot-devtools 模塊包含一個嵌入式 LiveReload 服務器,用於在資源發生更改時觸發瀏覽器刷新。

為了在瀏覽器中實現此功能,我們需要安裝 LiveReload 插件,其中一個實現是 Remote Live Reload (Chrome 擴展程序)。

5. 全局設置spring-boot-devtools 提供了一種配置不與任何應用程序耦合的全局設置的方法。該文件名為 .spring-boot-devtools.properties,位於 $HOME 目錄。

6. 遠程應用程序

6.1. 通過 HTTP 進行遠程調試(遠程調試隧道)

<spring-boot-devtools> 提供通過 HTTP 的原生遠程調試功能。要使用此功能,<spring-boot-devtools> 必須作為應用程序的一部分進行打包。這可以通過在 Maven 插件中禁用 <excludeDevtools> 配置來實現。

以下是一個快速示例:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>false</excludeDevtools>
            </configuration>
        </plugin>
    </plugins>
</build>

要通過 HTTP 進行遠程調試,需要執行以下步驟:

  1. 部署並啓動服務器上的應用程序,同時啓用遠程調試:
-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n

如我們所見,遠程調試端口在這裏未被提及。因此,Java 將會選擇一個隨機端口。

  • 對於相同的項目,請打開 啓動配置,選擇以下選項:
    選擇主類: org.springframework.boot.devtools.RemoteSpringApplication
    在程序參數中,添加應用程序的 URL,例如 http://localhost:8080
  • 通過 spring-boot 應用程序,調試器的默認端口為 8000,可以通過以下方式覆蓋:
    spring.devtools.remote.debug.local-port=8010
    1. 現在創建一個遠程調試配置,將端口設置為 8010,或通過屬性配置時使用的 8000(如果使用默認配置)

    以下是日誌的示例:

      .   ____          _                                              __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
     \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
      '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
     =========|_|==============|___/===================================/_/_/_/
     :: Spring Boot Remote ::  (v1.5.2.RELEASE)
    
    2017-03-12 22:24:11.089  ...: Starting RemoteSpringApplication v1.5.2.RELEASE on machine with PID 10476 (..\org\springframework\boot\spring-boot-devtools\1.5.2.RELEASE\spring-boot-devtools-1.5.2.RELEASE.jar started by user in project)
    2017-03-12 22:24:11.097  ...: No active profile set, falling back to default profiles: default
    2017-03-12 22:24:11.357  ...: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Sun Mar 12 22:24:11 IST 2017]; root of context hierarchy
    2017-03-12 22:24:11.869  ...: The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
    2017-03-12 22:24:11.949  ...: LiveReload server is running on port 35729
    2017-03-12 22:24:11.983  ...: Started RemoteSpringApplication in 1.24 seconds (JVM running for 1.802)
    2017-03-12 22:24:34.324  ...: Remote debug connection opened

    6.2. 遠程更新

    客户端會監視應用程序類路徑的變化,與遠程重啓功能類似。當類路徑發生任何更改時,更新後的資源會被推送到遠程應用程序,並觸發重啓。

    更改會推送到客户端運行狀態下,因為僅在客户端運行狀態下才能進行文件更改監控。

    以下是這在日誌中的表現:

    2017-03-12 22:33:11.613  INFO 1484 ...: Remote debug connection opened
    2017-03-12 22:33:21.869  INFO 1484 ...: Uploaded 1 class resource

    7. 結論

    通過這篇文章,我們已經展示瞭如何利用 spring-boot-devtools 模塊來改善開發人員的體驗並縮短開發時間,通過自動化大量任務。

  • user avatar
    0 位用戶收藏了這個故事!
    收藏

    發佈 評論

    Some HTML is okay.