英文:
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中的运行配置控制。
它说明:
- 安装Spring Boot Developer工具;
- 在
Run Configuration
中将On Update Action
设置为Hot swap classes and update trigger file if failed
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:
- Install Spring Boot Developer tools;
- Set the
On Update Action
inRun Configuration
toHot swap classes and update trigger file if failed
On Frame deactivation
have the class `Update Classes and Resources'
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论