SpringBoot热部署:
通过devtools可以实现项目的热部署,即页面修改后立即生效。
可以在application.propertis文件中配置关闭缓存,以实现对属性文件热部署。
#freemarker模板引擎关闭缓存
spring.freemarker.cahc=false
#thymeleaf模板引擎关闭缓存
spring.thymeleaf.cache=false
devtools会监听classpath下的文件修改变动,并在保存后立即重启应用。
1.pom.xml文件依赖及插件设置
SpringBoot 提供spring-boot-devtools模块来支持热部署,提高开发效率,无需手动重启SpringBoot应用,对其配置修改后无需重启项目即可实时生效,需添加以下配置:
<dependencies>
<!--引入Web模块-->
<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>
<!--springBoot热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<!--optional=true,依赖不会传递,该项目依赖devtools-->
<!--其他依赖此项目的项目如果需要devtools则需要重新引入,可以根据需求来选择-->
<optional>true</optional>
<scope>true</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--fork:如果没有该项配置,devtools不会起作用,即应用不会restart-->
<fork>
true
</fork>
</configuration>
</plugin>
</plugins>
</build>
2.IDEA配置
1.“File”–>“Setting”–>“Build,Execution,Deployment”–>“Compiler”,选中"Build project automatically",表示让项目自动编译。
2.快捷键"Ctrl+Alt+Shift+/",选择"Registry",把"compiler.automake.allow.when.app.running"打勾选中
3.application.properties使热部署生效
#页面不加载缓存,修改即时生效
#关闭缓存,及时刷新
#freemarker模板引擎关闭缓存
spring.freemarker.cahc=false
#thymeleaf模板引擎关闭缓存
spring.thymeleaf.cache=false
#开启热部署
spring.devtools.restart.enabled=true
#设置项目重启需要重新加载的目录文件
spring.devtools.restart.additional-paths=src/main/java
#排除项目重启是不需要重新加载的目录文件(可根据需求来设置)
spring.devtools.restart.exclude=不需要重新加载文件路径
4.测试
- 修改类->保存:应用自动重启
- 修改配置文件->保存:应用自动重启
- 修改页面->保存:应用不重启,但重新加载,同时页面刷新
SpringBoot热部署搞定!!
ps:第一次写有点小激动 欢迎交流