springboot 开发时热更新
springboot提供了spring-boot-devtools的依赖,可以做到修改java代码和springboot配置文件后工程立即自动重启,这样就避免了手动重启。非常方便
maven引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
添加一个maven插件
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
配置idea
File->Settings->Compiler
勾上如图所示的选项
使用ctrl + shift + alt + /快捷键,选择Registry
勾上这个,点击close
![](https://img2022.cnblogs.com/blog/426777/202203/426777-20220317113651467-1305070478.webp)
编辑application.yml文件
devtools:
livereload:
enabled: true
port: 9527
restart:
enabled: true
配置运行,点击 Edit Configurations
![](https://img2022.cnblogs.com/blog/426777/202203/426777-20220317113743346-483537019.webp)
将下面的选择项选为 update classes and resources
![](https://img2022.cnblogs.com/blog/426777/202203/426777-20220317113803579-1161168846.webp)
配置完成后启动工程,修改一处java代码后可见控制台:
![](https://img2022.cnblogs.com/blog/426777/202203/426777-20220317113819155-1492200123.webp)
因为devtools导致的异常
ScheduleJobEntity scheduleJob = (ScheduleJobEntity) context.getMergedJobDataMap()
.get(ScheduleJobEntity.JOB_PARAM_KEY);
代码报错
java.lang.ClassCastException: io.renren.modules.job.entity.ScheduleJobEntity cannot be cast to io.renren.modules.job.entity.ScheduleJobEntity
解决: 使用instanceof 关键字判断类型
Object obj = context.getMergedJobDataMap().get(ScheduleJobEntity.JOB_PARAM_KEY);
ScheduleJobEntity scheduleJob;
if(obj instanceof ScheduleJobEntity) {
scheduleJob = (ScheduleJobEntity) obj;
} else {
scheduleJob = JSON.parseObject(JSON.toJSON(obj).toString(), ScheduleJobEntity.class);
}