springboot + IDEA 项目热部署(自动刷新,免重启)[转]
用IDEA做springboot,可以通过热部署的方式快速展示。
spring为开发者提供了一个spring-boot-devtools模块,使应用支持热部署,提高开发者效率,无需手动重启springboot应用。
devtools的原理
深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。
devtools的方式 总的来说,一共需要两个步骤:
1、设置pom.xml文件,添加devtools依赖
<!--添加热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency>
在<build> 下面<plugins>里,maven-plugin添加配置
<plugin> <!--热部署配置--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork:如果没有该项配置,整个devtools不会起作用--> <fork>true</fork> </configuration> </plugin>
2、设置IDEA的自动编译:
(1)File-Settings-Compiler 勾选 Build Project automatically
(2)快捷键 ctrl + shift + alt + /,选择Registry,勾上 Compiler autoMake allow when app running
这样我们的热部署就完成了。
借鉴: