spring boot熱啟動有兩種方式
1. 以Maven插件的形式去加載,所以啟動時使用通過Maven命令mvn spring-boot:run啟動,而通過Application.run方式啟動的會無效,因為通過應用程序啟動時,已經繞開了Maven插件機制。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2. spring-boot-devtools
這種方式無論怎么啟動應用,都可以達到修改文件后重啟應用。
(1)、如果發現沒有熱部署效果,則需要檢查IDE配置中有沒有打開自動編譯。
(2)、如果使用Thymeleaf模板引擎,需要把模板默認緩存設置為false
#禁止thymeleaf緩存(建議:開發環境設置為false,生成環境設置為true) spring.thymeleaf.cache=false
(3)、針對devtools的可以指定目錄或者排除目錄來進行熱部署
#添加那個目錄的文件需要restart spring.devtools.restart.additional-paths=src/main/java #排除那個目錄的文件不需要restart spring.devtools.restart.exclude=static/**,public/**
(4)、默認情況下,/META-INF/maven,/META-INF/resources,/resources,/static,/templates,/public這些文件夾下的文件修改不會使應用重啟,但是會重新加載(devtools內嵌了一個LiveReload Server,當資源發生改變時,瀏覽器刷新)
(5)、在application.properties中配置spring.devtools.restart.enabled=false,此時restart類加載器還會初始化,但不會監視文件更新。在SprintApplication.run之前調用System.setProperty(“spring.devtools.restart.enabled”, “false”);可以完全關閉重啟支持。
