idea springboot專案熱更新
阿新 • • 發佈:2018-12-22
前言
在專案開發過程中,常常會改動頁面資料或者修改資料結構,為了顯示改動效果,往往需要重啟應用檢視改變效果。這種開發體驗無疑是很差的,Springboot為我們提供了devtools來幫助我們實現熱更新。
使用springboot提供的spring-boot-devtools
新增devtools依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional >true</optional>
</dependency>
springboot maven外掛配置
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- 如果沒有該配置,devtools不會生效 -->
</configuration>
</plugin>
application.properties配置
#熱部署生效
spring.devtools.restart.enabled=true
#設定重啟的目錄
spring.devtools.restart.additional-paths=src/main/java
#classpath目錄下的resources資料夾內容修改不重啟
spring.devtools.restart.exclude=src/main/resources
啟動
1.maven啟動方式:
在maven中使用springboot:run啟動
2.執行Springboot啟動類
說明:檔案內容有修改,要手動觸發一下idea的make,才會熱更新。
class只要有變動就會重啟,雖然Springboot重啟很快,但感覺也不是很好。
在idea整合的tomcat中執行Springboot
pom.xml中打包型別修改為war
<packaging>war</packaging>
maven新增spring-boot-starter-tomcat依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
應用啟動類修改
@SpringBootApplication
public class App extends SpringBootServletInitializer
{
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(App.class);
}
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
繼承SpringBootServletInitializer,重寫configure方法。
部署
跟常規的idea的web專案一樣即可。如圖:
後續頁面、資原始檔、class等修改都跟常規的idea的web專案一樣操作即可。這樣class只要沒有增加類、方法、修改簽名等就不用重啟tomcat了。