Eclipse Spring Boot實現熱部署

來源:互聯網
上載者:User

標籤:uil   內嵌   有用   重新整理   監聽   清空   情況   修改   clip   

spring為開發人員提供了一個名為spring-boot-devtools的模組來使Spring Boot應用支援熱部署,提高開發人員的開發效率,無需手動重啟Spring Boot應用。

devtools的原理

深層原理是使用了兩個ClassLoader,一個Classloader載入那些不會改變的類(第三方Jar包),另一個ClassLoader載入會更改的類,稱為restart ClassLoader,這樣在有代碼更改的時候,原來的restart ClassLoader 被丟棄,重新建立一個restart ClassLoader,由於需要載入的類相比較少,所以實現了較快的重啟時間。

使用需要添加以下的配置:

<dependencies>
<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-web</artifactId>
</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>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

 

必須設定以下內容:

eclipse –> Project –> Build Automatically 要選中,不選中的話不起作用。

說明:

(1) devtools可以實現頁面熱部署(即頁面修改後會立即生效,這個可以直接在application.properties檔案中配置spring.thymeleaf.cache=false來實現),
實作類別檔案熱部署(類檔案修改後不會立即生效),實現對屬性檔案的熱部署。
即devtools會監聽classpath下的檔案變動,並且會立即重啟應用(發生在儲存時機),注意:因為其採用的虛擬機器機制,該項重啟是很快的
(2)配置了後在修改java檔案後也就支援了暖開機,不過這種方式是屬於項目重啟(速度比較快的項目重啟),會清空session中的值,也就是如果有使用者登陸的話,項目重啟後需要重新登陸。

預設情況下,/META-INF/maven,/META-INF/resources,/resources,/static,/templates,/public這些檔案夾下的檔案修改不會使應用重啟,但是會重新載入(devtools內嵌了一個LiveReload server,當資源發生改變時,瀏覽器重新整理)。

devtools的配置

在application.properties中配置spring.devtools.restart.enabled=false,此時restart類載入器還會初始化,但不會監視檔案更新。
在SprintApplication.run之前調用System.setProperty(“spring.devtools.restart.enabled”, “false”);可以完全關閉重啟支援,配置內容:

 

#熱部署生效
spring.devtools.restart.enabled: true
#設定重啟的目錄
#spring.devtools.restart.additional-paths: src/main/java
#classpath目錄下的WEB-INF檔案夾內容修改不重啟
spring.devtools.restart.exclude: WEB-INF/**

 

測試

  • 修改類–>儲存:應用會重啟
  • 修改設定檔–>儲存:應用會重啟
  • 修改頁面–>儲存:應用不會重啟,但會重新載入,頁面會重新整理(原理是將spring.thymeleaf.cache設為false,參考:Spring Boot配置模板引擎)

Eclipse Spring Boot實現熱部署

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.