IntelliJ IDEA热部署的解决方法
intellij Idea热部署
第一次用intellij Idea。感觉真的不错,界面漂亮,黑色的主题尤其看着舒服。
创建了springboot项目,为了热启动加入了spring-boot-devtools,跑起来后,发现保存修改过的文件时,并没有自动热部署。
按照网上搜索到的方法,
设置自动编译等,在application.properties开启spring.devtools.restart.enabled=true spring.devtools.restart.additional-paths=src/main/java 等。即使springboot自动重启了,但加载的仍然是修改前的类。
然后再spring官网查看了一下,看到:
Triggering a restart
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.
If using a build plugin, running mvn compile for Maven or gradle build for Gradle will trigger a restart.
意思就在IntelliJ IDEA中需要手动编译,就是点击菜单build,然后点击build project。
IntelliJ IDEA即使设置了自动编译,但在运行状态时是不管用的。看下图,红框右侧写着only works while not running/debugging,意思是运行时或调试时无效。
经过测试,手动编译后,确实是热启动,即只重新加载了classpath下的类,而不是全部。
在controler中,写入一个方法
@Controller
public class TestController {
@GetMapping("test")
@ResponseBody
public String showTest(){
System.out.println("String loader="+String.class.getClassLoader());
System.out.println("org.springframework.stereotype.Controller loader="+Controller.class.getClassLoader());
System.out.println("TestControler loader="+this.getClass().getClassLoader());
return "hello, I am test";
}
}
启动springboot后,打开http://localhost:8080/test页面后输出
string loader=null
org.springframework.stereotype.Controller loader=jdk.internal.loader.ClassLoaders$AppClassLoader@2437c6dc
TestControler loader=org.springframework.boot.devtools.restart.classloader.RestartClassLoader@418fe081
修改TestController后并编译后,打开http://localhost:8080/test页面后输出
string loader=null
org.springframework.stereotype.Controller loader=jdk.internal.loader.ClassLoaders$AppClassLoader@2437c6dc
TestControler loader=org.springframework.boot.devtools.restart.classloader.RestartClassLoader@37c9506c
可以看到org.springframework.stereotype.Controller的classloader没有变,TestControler的classloader有变化。