Springboot implements hot deployment in two ways!


Springboot implements hot deployment in two ways!


As a java programmer, otaku often needs to modify the code in his daily work, and then restart the service to verify whether the code takes effect. If it’s a small project, it’s OK. The restart speed is relatively fast and the waiting time is relatively short. However, as the project becomes larger and is split into multiple services, changing some code may require restarting multiple services to take effect. In this way, a lot of time is spent waiting for the service to restart.

This will certainly not work, which has greatly affected my development efficiency. Is there a way to implement it without restarting the project after modifying the code?

There must be, otherwise how did this article come from.

Hot swap

From Java 1 Since 4, the JVM has introduced HotSwap, which can update the bytecode of the class during debug. Therefore, using hot deployment, you can load the modified code without restarting the service after modifying the code, but it can only be used to update the method body. As an artifact, idea naturally supports this technology.

Configure idea

  click the currently running service, and then clickEdit Configurations

Springboot implements hot deployment in two ways!

  click the program to be configured and findOn ‘Update’ actionandOn frame deactivationchoiceUpdate classes and resources。 Click OK to implement the hot deployment.

Springboot implements hot deployment in two ways!

  after the above configuration, after modifying the code. Just click on the small hammer or use the shortcut keyCommand + F9Recompile and make the changed code take effect. It will also prompt how many classes have been re read.

Springboot implements hot deployment in two ways!

Springboot implements hot deployment in two ways!

Although the function of hot deployment can be realized here. However, the Java virtual machine can only realize the modification and hot deployment of the method body. For the structural modification of the whole class, you still need to restart the virtual machine and reload the class to complete the update operation.


Initial state

Springboot implements hot deployment in two ways!

Method body modification

Springboot implements hot deployment in two ways!

Class structure change

Because hot deployment only supports modifying the method body, an error will be reported when the class structure is changed, and whether to restart is prompted.

Springboot implements hot deployment in two ways!


Although a simple hot deployment is realized by configuring the idea, it has obvious disadvantages. It can only modify the method body for hot deployment. Obviously, it can’t meet the daily needs, so we need to use devtools instead.

   devtools is a software calledspring-boot-devtoolsTo enable spring boot applications to support hot deployment and improve developers’ development efficiency without manually restarting spring boot applications. It is very simple to use. You only need to introduce the following dependencies into the project.


Trigger restart

  devtools is not strictly speakingHot deployment, butQuick restart。 But why do you say that? The implementation principle of devtools is to use two class loaders, one isbase classloaderTo load classes that will not be changed (for example, jars from third parties), and another isrestart classloaderUsed to load the class currently under development. So when the application restarts,restart classloaderWill be discarded and a new class loader created. This means that an application restart is usually much faster than a “cold start” becausebase classloaderIt has been filled and is available.

  in short:By monitoring the classpath resources, when the files on the classpath are changed, the application will be restarted automatically. Because only the modified classes need to be re read, it is faster than cold start

So the question is, how to update the classpath to trigger automatic restart? In fact, it depends on the IDE you use:

  1. In eclipse, saving the modified file causes the classpath to be updated and triggers a restart.
  2. In IntelliJ idea, you need to click the build buttonCommand + F9Build projects to implement.

Configure automatic restart

At this time, a little partner may want to ask, isn’t idea similar to the function of automatically triggering restart by saving files in eclipse. There must be. You only need to configure it in the following two steps.

Note: you need to restore all the previous settings.

  1. openBuild project automatically

Springboot implements hot deployment in two ways!

  2. Use shortcut keys:Ctrl + Alt + Shift + /Call up the registry window and check itcompiler.automake.allow.when.app.runningOptions.

The new version is shown in the figure below:

Springboot implements hot deployment in two ways!


Idea can only realize the modification and hot deployment of method body, which can not meet the daily use requirements, so devtools is more recommended. But if you think restarting is not fast enough for you. You can consider using the jrebel plug-in.


If you think it’s helpful to you, you can comment and praise more. You can also go to my home page. Maybe there are articles you like, you can also pay attention to them. Thank you.

I am a different technology house. I make a little progress every day and experience a different life. See you next time!

Recommended Today

The whole tutorial of docker installation and use, installation / complete command / dockerfile image production / docker container arrangement and one click installation of nginx + redis + MySQL / visualizer portal (version 2022)

官网: https://docs.docker.com/ 官网: https://www.docker.com/ docker 镜像市场: https://hub.docker.com/ 一、docker 说明 1.1、docker 核心 1、Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源,Docker 是一个 CS 架构软件。 2、Docker 是一个虚拟化轻量级linux服务器,可以解决我们在开发环境中运行配置问题 3.、Docker的主要目标是‘build ,ship and run any app,anywhere’,一次封装,到处运行 4、容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。 1.2、docker 版本问题 .Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。 1.3、docker 架构( 3大核心) · 1、Images 镜像 (等于软件) · 2、Registry […]