简体   繁体   中英

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. 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.

I tried as follows without success:

  • added the maven spring-boot-devtools dependency
  • Settings > Build project automatically [yes]
  • Registry: complier.automake.allow.when.app.running [true]
  • Run Configuration > Update Policies: 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 . 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. I just want to have the java code reloaded.

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:

The restart technology provided by Spring Boot works by using two classloaders. Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. 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 . Spring Boot documentation states the same:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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