preface
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
。
click the program to be configured and findOn ‘Update’ action
andOn frame deactivation
choiceUpdate classes and resources
。 Click OK to implement the hot deployment.
after the above configuration, after modifying the code. Just click on the small hammer or use the shortcut keyCommand + F9
Recompile and make the changed code take effect. It will also prompt how many classes have been re read.
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.
test
Initial state
Method body modification
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.
DevTools
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-devtools
To 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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
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 classloader
To load classes that will not be changed (for example, jars from third parties), and another isrestart classloader
Used to load the class currently under development. So when the application restarts,restart classloader
Will be discarded and a new class loader created. This means that an application restart is usually much faster than a “cold start” becausebase classloader
It 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:
- In eclipse, saving the modified file causes the classpath to be updated and triggers a restart.
- In IntelliJ idea, you need to click the build button
Command + F9
Build 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
。
2. Use shortcut keys:Ctrl + Alt + Shift + /
Call up the registry window and check itcompiler.automake.allow.when.app.running
Options.
The new version is shown in the figure below:
summary
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.
ending
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!