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

 

编辑application.yml文件

devtools:
  livereload:
    enabled: true
    port: 9527
  restart:
    enabled: true

配置运行,点击 Edit Configurations 

 

 

将下面的选择项选为 update classes and resources 

 

 

配置完成后启动工程,修改一处java代码后可见控制台:

 

 

 

因为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);
}

posted on 2022-03-17 11:32  &大飞  阅读(708)  评论(0编辑  收藏  举报

导航