热启动就需要用到我们在一开始就引入的另外一个组件:spring-boot-devtools。它是 Spring Boot 提供的一组开发工具包,其中就包含我们需要的热部署功能,在使用这个功能之前还需要再做一些配置。
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-Devtools</artifactId>
<optional>true</optional>
在 plugin 中配置另外一个属性 fork,并且配置为 true。
<build>
<plugins><plugin>
<groupId>org.springframework.boot
<artifactId>spring-boot-maven-plugin
<configuration>
<fork>true
</configuration></plugin>
</plugins>
</build>
OK,以上的配置就完成了,如果你使用的是 Eclipse 集成开发环境,那么恭喜你大功告成了;如果你使用的是 IDEA 集成开发环境,那么还需要做以下配置。
配置 IDEA
选择 File | Settings | Compiler 命令,然后勾选 Build project automatically 复选框,低版本的 IDEA 请勾选 make project automatically 复选框。
使用快捷键 Ctrl + Shift + A,在输入框中输入 Registry,勾选 compile.automake.allow.when.app.running 复选框:
全部配置完成后,IDEA 就支持热部署了,大家可以试着去改动一下代码就会发现 Spring Boot 会自动重新加载,再也不需要手动单击重新部署了。