Spring Boot – 在不重启Tomcat服务器的情况下更新类

huangapple go评论121阅读模式
英文:

Springboot - update classes without restarting Tomcat Server

问题

在我的Spring Boot(2.6.4)项目中,我想知道是否有可能对类(例如实体、DTO或控制器)进行更改,而无需重新启动整个应用程序(就像这里)。

我的环境包括:
IDE:Intellij Idea 2023 Ultimate
构建工具:Gradle

我已在Intellij中启用了以下选项:

  • 设置->构建、执行、部署->编译器:自动构建项目(已选中)
  • 在编译器的高级设置下启用选项“即使开发中的应用程序正在运行,也允许自动构建”

在Gradle中,我尝试了以下两个选项:

developmentOnly 'org.springframework.boot:spring-boot-devtools'

compileOnly 'org.springframework.boot:spring-boot-devtools'

我正在使用Intellij中的绿色调试图标在调试模式下调试项目。

然后,我尝试在调试运行项目时对DTO类进行更改,例如:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class CarDto {
    @JsonProperty("carId")
    private Long carId;
    //@JsonProperty("name")
    //private String name;
}

通过注释掉这两行:

//@JsonProperty("name")
//private String name;

但是在Intellij的终端上没有任何指示表明它已经检测到了更改并重新加载了更改后的类。

更新

另一篇较新的文章说明了它由Intellij中的运行配置控制。

它说明:

  1. 安装Spring Boot Developer工具;
  2. Run Configuration中将On Update Action设置为Hot swap classes and update trigger file if failed
  3. On Frame deactivation设置为Update Classes and Resources

问题: 如何使实时重新加载工作 - 看起来有几组不同的说明。

如果需要,我可以发布更多的build.gradle内容。

英文:

In my Spring Boot (2.6.4) project I would like to know if it is possible to make changes to a class (for example entity, DTO or controller) and have the changed classes reload without having to restart the whole application (like here)

My environment consists of:
IDE: Intellij Idea 2023 Ultimate
Build Tool: Gradle

I have enabled the options in Intellij for:

  • Settings->Build, Execution, Deployment->Compiler : Build Project Automatically (ticked)
  • Enabled option 'allow auto-make to start even if developed application is currently running' in Settings -> Advanced Settings under compiler

In gradle I have tried both options:

developmentOnly 'org.springframework.boot:spring-boot-devtools'
and
compileOnly 'org.springframework.boot:spring-boot-devtools'

I am Debugging the project in Intellij using the Green Debug icon on the tool menu.

I then tried to make a change to a Dto class while running the project in Debug, for example:

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class CarDto {
    @JsonProperty("carId")
    private Long carId;
    @JsonProperty("name")
    private String name;
}

By commenting out the two lines:

//@JsonProperty("name")
//private String name;

But there is no indication on the terminal in Intellij that it has detected the changes and reloaded the changed classes.

UPDATED

Another more recent post states that it is controlled by the run configuration in Intellij.

It states:

  1. Install Spring Boot Developer tools;
  2. Set the On Update Action in Run Configuration to Hot swap classes and update trigger file if failed
  3. On Frame deactivation have the class `Update Classes and Resources'

Spring Boot – 在不重启Tomcat服务器的情况下更新类

Q: How can I get the live reload to work - it appears there is a few different sets of instructions.

I can post more of the build.gradle if needed.

答案1

得分: 1

你只是在调试时更改类,还是计划在运行中的服务器上更新应用程序。
Intellij在Spring Boot被发明之前就已经有了热重载功能,所以我个人不使用 spring-boot-devtools,只需在调试器中启动应用程序,然后按下 Shift + F9(旧的热键)重新编译和重新加载JVM的类。

英文:

Are you just changing the classes while debugging, or are you you planning to update the application on a running server.
Intellij has had hot-reload, 10+ years before Spring boot was invented, so I personally don't use spring-boot-devtools, just start the application in the debugger and, do shit+F9 (old hotkey) to recompile and reload the classes of the JVM.

huangapple
  • 本文由 发表于 2023年8月5日 09:51:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定