简体   繁体   English

在Spring-Boot项目中,Intellij中的Hottowap代码热销吗?

[英]Hot to HotSwap code in Intellij in a Spring-Boot project?

I'm using spring-boot-starter for a larger server project that opens a static socket for communication. 我正在将spring-boot-starter用于较大的服务器项目,该项目将打开一个静态套接字进行通信。 For not always having to restart the server in Intellij IDEA after every code change (and thereby quit the socket connection), I'm trying to HotSwap the changed *.java code. 对于总是不重新启动服务器Intellij IDEA每次代码更改后(并因此退出套接字连接),我想热插拔的改变*的.java代码。

I tried as follows without success: 我尝试如下,但未成功:

  • added the maven spring-boot-devtools dependency 添加了Maven spring-boot-devtools依赖项
  • Settings > Build project automatically [yes] 设置> Build project automatically [yes]
  • Registry: complier.automake.allow.when.app.running [true] 注册表: complier.automake.allow.when.app.running [true]
  • Run Configuration > Update Policies: Update classes and resources 运行配置>更新策略: Update classes and resources

BUT: whenever I modify a class, my application restarts automatically. 但是:每当我修改类时,我的应用程序都会自动重新启动。 Of course this will also reload the classes, but then I could also trigger the restart myself at will. 当然,这也将重新加载类,但随后我也可以自行触发重新启动。 I'm not gaining anything by automated restarts. 我没有通过自动重启获得任何收益。

So I disabled the restarts via spring.devtools.restart.enabled=false . 所以我通过spring.devtools.restart.enabled=false禁用了重启。 But then the code swap does not work until the server is restarted. 但是,直到服务器重新启动后,代码交换才起作用。

But is it possible at all without explicit restarts? 但是,如果没有显式重新启动,有可能吗?

Sidenote: I'm not about hotswap a web-UI in the browser. 旁注:我不是要在浏览器中热交换Web UI。 I just want to have the java code reloaded. 我只想重新加载Java代码。

That's how Spring Boot dev tools work, every change requires a restart but the restart with dev tools is much faster because it uses custom class loader: 这就是Spring Boot开发工具的工作方式,每次更改都需要重新启动,但是使用dev工具重新启动要快得多,因为它使用了自定义类加载器:

The restart technology provided by Spring Boot works by using two classloaders. Spring Boot提供的重启技术通过使用两个类加载器来工作。 Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. 不变的类(例如,来自第三方jar的类)将被加载到基类加载器中。 Classes that you are actively developing are loaded into a restart classloader. 您正在积极开发的类将加载到重新启动类加载器中。 When the application is restarted, the restart classloader is thrown away and a new one is created. 重新启动应用程序时,将丢弃重新启动类加载器,并创建一个新的类加载器。 This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated. 这种方法意味着应用程序的重启通常比“冷启动”要快得多,因为基本类加载器已经可用并已填充。

If you want to hotswap code without restarting then one option is to use JRebel . 如果要热交换代码而不重新启动,则一种选择是使用JRebel Spring Boot documentation states the same: Spring Boot文档指出了相同的内容:

If you find that restarts are not quick enough for your applications or you encounter classloading issues, you could consider reloading technologies such as JRebel from ZeroTurnaround. 如果发现重新启动对于您的应用程序来说不够快,或者遇到类加载问题,则可以考虑从ZeroTurnaround重新加载技术,例如JRebel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM