步骤很简单,只需要变更一下pom文件。
1. Maven添加依赖
将下边的依赖添加到你的pom文件中去
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
2. 实现热加载
- 在Eclipse中,保存文件会自动触发热加载
- 在IDEA中,需要build project来触发热加载,快捷键是Ctrl+F9
官方文档:As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. The way in which you cause the classpath to be updated depends on the IDE that you are using. In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart. In IntelliJ IDEA, building the project (Build -> Build Project) has the same effect.
3. 注意
- 如果你的项目是通过java命令(java -jar)或者某个特殊的类加载器启动的,spring-boot 的Developer tools会认为这是生产环境而自动失效