SpringBoot應用 Automatic Restart以及靜態資源 livereload 設定

Haiyoung發表於2017-11-21
SpringBoot應用 Automatic Restart以及靜態資源 livereload 設定
20.2 Automatic Restart
Applications that use spring-boot-devtools automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE, as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a folder is monitored for changes. Note that certain resources, such as static assets and view templates, do not need to restart the application.

當類路徑上的檔案發生變化時,使用spring-boot-devtools的應用程式會自動重啟。 當在IDE中工作時,它為程式碼更改提供了一個非常快速的反饋迴圈,這是一個有用的功能。 預設情況下,路徑下的所有條目都將被監視是否更改。 但是,某些時候,有些資源(如靜態資產和檢視模板)的更改不需要重新啟動應用程式。

20.2.1 Excluding Resources
Certain resources do not necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can be edited in-place. By default, changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates does not trigger a restart but does trigger a live reload. If you want to customize these exclusions, you can use the spring.devtools.restart.exclude property. For example, to exclude only /static and /public you would set the following property:

有些資源被更改時不需要觸發服務重啟。例如,Thymeleaf模板可以就地編輯而不觸發服務重啟。預設情況下,更改 /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates 這些目錄下的資源不會觸發重啟。也可以通過設定spring.devtools.restart.exclude屬性來自定義這些目錄。例如,將 spring.devtools.restart.exclude 設定為如下可排除對
/static,/public 目錄的restart監測。

spring.devtools.restart.exclude=static/**,public/**

20.2.3 Disabling Restart

If you do not want to use the restart feature, you can disable it by using the spring.devtools.restart.enabled property. In most cases, you can set this property in your application.properties (doing so still initializes the restart classloader, but it does not watch for file changes).

如果您不想使用重新啟動功能,則可以使用spring.devtools.restart.enabled屬性將其禁用。 在大多數情況下,您可以在application.properties中設定此屬性(這樣做仍會初始化重新啟動類載入器,但不會監視檔案更改)。

If you need to completely disable restart support (for example, because it doesn’t work with a specific library), you need to set the spring.devtools.restart.enabled System property to false before calling SpringApplication.run(…​), as shown in the following example:

如果您需要完全禁用重新啟動支援(例如,因為它不適用於特定的庫),則需要在呼叫SpringApplication.run(...)之前將spring.devtools.restart.enabled 系統屬性設定為false, 如以下示例所示:

public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApp.class, args);
}


20.3 LiveReload
The spring-boot-devtools module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed. LiveReload browser extensions are freely available for Chrome, Firefox and Safari from livereload.com.

spring-boot-devtools模組包含一個嵌入式LiveReload伺服器,當資源發生變化時,可用於觸發瀏覽器重新整理。 LiveReload瀏覽器擴充套件支援的Chrome,Firefox和Safari,可從livereload.com免費獲得。

If you do not want to start the LiveReload server when your application runs, you can set the spring.devtools.livereload.enabled property to false.

如果您不想在應用程式執行時啟動LiveReload伺服器,則可以將spring.devtools.livereload.enabled屬性設定為false。

[Note]
You can only run one LiveReload server at a time. Before starting your application, ensure that no other LiveReload servers are running. If you start multiple applications from your IDE, only the first has LiveReload support.

一次只能執行一個LiveReload伺服器。 在開始您的應用程式之前,請確保沒有其他LiveReload伺服器正在執行。 如果從IDE啟動多個應用程式,則只有第一個支援LiveReload。

進行相關設定之前,要引入 spring-boot-devtools 依賴。

相關文章