As a Java programmer, Xiaozhai often needs to modify the code in his daily work, and then restart the service to verify whether the code takes effect. If it is a small project, the restart speed is faster and the waiting time is shorter. But as the project grows larger and is split into multiple services, some code changes may require restarting multiple services to take effect. In this way, it takes a lot of time to wait for the service to restart.
This will definitely not work, and it will greatly affect my development efficiency, so is there a way to achieve it, after modifying the code, can I not restart the project?
There must be some, otherwise the article is coming from 😁.
Since Java1.4, JVM has introduced HotSwap, which can update the bytecode of the class during Debug. So 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. IDEA as an artifact naturally supports this technology.
Click the currently running service, and then click
Edit Configurations .
Click on the program you want to configure, find
On ‘Update’ action and
On frame deactivation select
Update classes and resources . Click OK to implement hot deployment.
After the above configuration, after modifying the code. Just click the small hammer or use the shortcut key
Command + F9 to recompile, and the changed code can take effect. And it will also prompt how many classes have been re-read.
Although the function of hot deployment can be achieved here. However, the Java virtual machine can only implement the hot deployment of method body modification. For the structural modification of the entire class, it is still necessary to restart the virtual machine and reload the class to complete the update operation.
Method body modification
Class structure changes
Since hot deployment only supports modifying the method body, an error will be reported when the class structure changes, and it will be prompted whether it needs to be restarted.
Although the simple hot deployment was achieved by configuring IDEA, there are obvious shortcomings, and only the hot deployment of the method body can be modified. Obviously, it can't meet the daily needs, so DevTools needs to be used instead at this time.
DevTools is that Spring provides developers with a
spring-boot-devtools to enable Spring Boot applications to support hot deployment and improve developer development efficiency without manually restarting Spring Boot applications. It is very simple to use, just introduce the following dependencies into the project.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency>
In the strict sense, does not count as 16190c210f0f19 hot deployment , but fast restart . why would you said this? The implementation principle of DevTools is: two class loaders are used, one is
base classloader to load classes that will not be changed (for example, Jar from a third party), and the other is
restart classloader to load classes that are currently being developed. So when the application restarts,
restart classloader will be discarded and a new class loader will be created. This means that the application restart is usually much faster than a "cold start" because
base classloader already filled and available.
In short: monitors classpath resources. When files on the classpath change, the application is automatically restarted. Since 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 an automatic restart? Actually this depends on the IDE you use:
- In Eclipse, saving the modified file will cause the classpath to be updated and trigger a restart.
- In IntelliJ IDEA, you need to click the Build button
Command + F9build the project.
Configure automatic restart
At this time, some friends may want to ask, is there no function similar to that of saving files in Eclipse to automatically trigger a restart. It must be there, and it can be achieved by only performing the following two steps of configuration.
Note: You need to restore all the previous settings.
1. Turn on
Build project automatically .
2. Use the shortcut key:
Ctrl + Alt + Shift + / call up the Registry window, check the
The new version is shown below:
IDEA can only modify the hot deployment of the method body, which cannot meet the daily use requirements, so DevTools is more recommended. But if you feel that restarting is not fast enough for you. You can consider using the JRebel plugin.
If you think it is helpful to you, you can comment more and like it a lot, or you can go to my homepage to see, maybe there are articles you like, or you can just follow 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!